将自定义模型传递到 Umbraco 局部视图并获取对象投射错误

Passing a custom model into a Umbraco Partial View and getting object casting error

我正在尝试创建一个自定义模型并将其传递到我的部分视图,但我一直收到此错误 Unable to cast object of type 'Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedContent' to type 'BlogPostModel' 并且无法理解原因。我有 3 个模型,BlogPostModel 继承自 HomeModel,HomeModel 继承自 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; }
}

我的HomeController.cs是

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);
    }
}

这是我要在其上呈现部分视图的主页

@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>

我的部分观点是

@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>

我不明白为什么这不起作用。请帮忙

问题出在您的这部分代码中:

@foreach (var blogPost in blogLandingPage.Children)
{
      Html.RenderPartial("HomeBlogList", blogPost);
}

变量 blogPost 的类型为 'XmlPublishedContent',但是您的局部视图需要一个 TestUmbraco.Models.BlogPostModel 视图模型。

要么将局部视图中的模型更改为 XmlPublishedContent,要么填充模板中的正确类型 (BlogPostModel) 并将其传递给局部视图。您可能必须更改 BlogPostModel class 构造函数以接受 XmlPublishedContent 参数而不是 RenderModel 并使用该内容实例设置任何属性。