问题
I am trying to create a custom model and pass it to my partial view but I keep getting this error Unable to cast object of type 'Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedContent' to type 'BlogPostModel'
and cant understand why. I have 3 models, BlogPostModel which inherits from HomeModel which inherits from BaseLayoutModel.
BlogPostModel.cs
public class BlogPostModel : HomeModel
{
public BlogPostModel(RenderModel model) : base(model)
{
}
public IPublishedProperty MainBlogImage { get; set; }
public IPublishedProperty ImageAltText { get; set; }
public IPublishedProperty Introduction { get; set; }
public IPublishedProperty Category { get; set; }
}
HomeModel.cs
public class HomeModel : BaseLayoutModel
{
public HomeModel(RenderModel model) : base(model)
{
}
public IPublishedProperty SiteName { get; set; }
}
BaseLayoutModel.cs
public class BaseLayoutModel : RenderModel
{
public BaseLayoutModel(RenderModel model) : base(model.Content, model.CurrentCulture)
{
}
public IPublishedProperty PageTitle { get; set; }
public IPublishedProperty MainContent { get; set; }
public IPublishedProperty MetaDescription { get; set; }
public IPublishedProperty MetaKeywords { get; set; }
}
My HomeController.cs is
public class HomeController : RenderMvcController
{
public ActionResult Home(RenderModel CurrentItem)
{
var Model = new BlogPostModel(CurrentItem);
//Base Layout Model
Model.PageTitle = CurrentItem.Content.GetProperty("pageTitle");
Model.MainContent = CurrentItem.Content.GetProperty("mainContent");
Model.MetaDescription = CurrentItem.Content.GetProperty("metaDescription");
Model.MetaKeywords = CurrentItem.Content.GetProperty("metaKeywords");
Model.SiteName = CurrentItem.Content.GetProperty("siteName", recurse: true);
return View(Model);
}
}
This is my home page that I want to render partial view on
@inherits Umbraco.Web.Mvc.UmbracoViewPage<TestUmbraco.Models.BlogPostModel>
@{
Layout = "BaseLayout.cshtml";
IPublishedContent blogLandingPage = Umbraco.Content(1062);
}
<div class="row">
<div class="col-sm-12">
<section>
@foreach (var blogPost in blogLandingPage.Children)
{
Html.RenderPartial("HomeBlogList", blogPost);
}
</section>
</div>
</div>
And my partial view is
@inherits Umbraco.Web.Mvc.UmbracoViewPage<TestUmbraco.Models.BlogPostModel>
<article class="blog-teaser clearfix">
<div class="hovereffect teaser-image col-lg-4 col-md-4 col-sm-6 col-xs-12">
<a href="@Model.Content.Url">
<img src="@Umbraco.TypedMedia(Model.MainBlogImage).GetCropUrl(500,300)" alt="Placeholder to be Removed" />
</a>
<div class="teaser-overlay">
<h2>@Model.Category</h2>
<p>
<a class="info" href="/blogs/moyou-london-stamping-plate-review"> Read More</a>
</p>
</div>
</div>
<div class="teaser-text col-lg-8 col-md-8 col-sm-6 col-xs-12">
<header>
<h2>
<a rel="bookmark" href="@Model.Content.Url">@Model.PageTitle</a>
</h2>
</header>
<footer>
<p class="author-datetime">
<span rel="author" content="@Model.Content.CreatorName">
<a rel="nofollow" href="#">@Model.Content.CreatorName</a>
<time datetime="@Model.Content.CreateDate">@Model.Content.CreateDate.ToLongDateString(), @Model.Content.CreateDate.ToShortTimeString()</time>
</span>
</p>
</footer>
<section>
@Umbraco.Truncate(Model.Introduction.ToString(), 240, true)
<a class="more-link" href="@Model.Content.Url">Read more</a>
</section>
</div>
</article>
I cant understand why this isnt working. Please help
回答1:
The problem is in this part of your code:
@foreach (var blogPost in blogLandingPage.Children)
{
Html.RenderPartial("HomeBlogList", blogPost);
}
The variable blogPost
is of type 'XmlPublishedContent', your partial view however expects a TestUmbraco.Models.BlogPostModel
view model.
Either change the model in your partial view to XmlPublishedContent
, or populate and pass the correct type (BlogPostModel
) from your template to the partial. You might have to change the BlogPostModel
class constructor to accept an XmlPublishedContent
parameter instead of RenderModel
and set any properties using that content instance.
来源:https://stackoverflow.com/questions/34296463/passing-a-custom-model-into-a-umbraco-partial-view-and-getting-object-casting-er