Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Harnam Chana 2 posts 72 karma points
    Dec 15, 2015 @ 18:13
    Harnam Chana
    0

    Error when passing a custom model into a Umbraco Partial View

    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 'custom.Model' 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

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies