将自定义模型传递到Umbraco局部视图中并获取对象投射错误 [英] Passing a custom model into a Umbraco Partial View and getting object casting error

查看:67
本文介绍了将自定义模型传递到Umbraco局部视图中并获取对象投射错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自定义模型并将其传递给我的局部视图,但我一直收到此错误Unable to cast object of type 'Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedContent' to type 'BlogPostModel',无法理解原因.我有3个模型,BlogPostModel继承自HomeModel,HomeModel继承自BaseLayoutModel.

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

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

HomeModel.cs

public class HomeModel : BaseLayoutModel
{
    public HomeModel(RenderModel model) : base(model)
    {
    }

    public IPublishedProperty SiteName { get; set; }
}

BaseLayoutModel.cs

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是

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>

我的局部观点是

@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

推荐答案

问题出在代码的这一部分:

The problem is in this part of your code:

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

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

The variable blogPost is of type 'XmlPublishedContent', your partial view however expects a TestUmbraco.Models.BlogPostModel view model.

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

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.

这篇关于将自定义模型传递到Umbraco局部视图中并获取对象投射错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆