Copied to clipboard

Flag this post as spam?

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


  • Ado 8 posts 77 karma points
    Dec 16, 2015 @ 22:08
    Ado
    0

    UmbracoTemplateview inheritance in master.cshtml overrides UmbracoViewPage<customModel> in blog.cshtml

    So I need to be able to set the title of my site to the page the visitor is on so in my Master.cshtml I have

    @inherits UmbracoTemplatePage
    ....
    <title>@Model.Content.Name</title>
    

    Works great.

    Now I want to pass my custom blogpost model to my blogview which has Master.cshtml as layout

        @inherits UmbracoViewPage<Umbraco_Blog.Models.BlogPost[]>
        @{
        Layout = "Master.cshtml";
          }
    

    and in my controller i return a BlogPost[]

    return CurrentTemplate(blogposts);
    

    But this just gives me this error

    The model item passed into the dictionary is of type 'Umbraco_Blog.Models.BlogPost[]', but this dictionary requires a model item of type 'Umbraco.Web.Models.RenderModel'.

    If i remove the layout, then it works.

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Dec 17, 2015 @ 07:52
    Dave Woestenborghs
    0

    Hi Ado,

    How do you pass your model to the view ? Can you post some more code. THat will be easier to help find a solution for your problem.

    Dave

  • Ado 8 posts 77 karma points
    Dec 17, 2015 @ 14:01
    Ado
    0

    Hi Dave, the problem was that I was passing an array of Blogpost to my view, but the Master.cshtml doesnt accept arrays, it accepts a regular BlogPost object but not an array.

    In my blog solution I made before I tried umbraco, I pass an array of BlogPost to my view. It works without a problem because I dont pass anything into my master.cshtml there.

    I know I can just pass the parent model Blog, to my view and get the children from the view. But I want to that from my controller, and pass all the blogposts from there.

    Now this code below works fine, where I pass the parent of BlogPosts, Blog, into the view.

    @inherits UmbracoViewPage<Blog>
        @foreach (var item in Model.Content.Children.Where("Visible").OrderBy("CreateDate desc"))
        {
            <h1><a href="@item.Url">@item.Name</a></h1>
                <p class="text-capitalize">@item.CreateDate.ToString("dd MMMM yyyy HH:mm")</p>
                <p>@item.GetProperty("blogPostBody")</p>
                <p class="text-right text-capitalize"> @item.CreatorName</p>
                <hr />
    
    
        }
    

    But I want to be able to work with my custom BlogPosts model in the view. I want to pass all the children of the Blog as my custom BlogPost model to the view. So I can use my properties from my custom BlogPost model something like this

    @inherits UmbracoViewPage<BlogPost[]>
    @foreach (var item in Model)
    {
        <h1><a href="@item.Content.Url">@item.Name</a></h1>
            <p class="text-capitalize">@item.Content.CreateDate.ToString("dd MMMM yyyy HH:mm")</p>
            <p>@item.blogPostBody</p>
            <p class="text-right text-capitalize"> @item.Content.CreatorName</p>
            <hr />
    

    }

    What would be even better is if there was a way to dynamically set the title and other stuff in my Master.cshtml without using this part

    @inherits UmbracoTemplatePage
    

    I know i can just pass everything in a viewbag, but then I would need to create the same viewbags in every action.

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Dec 17, 2015 @ 14:07
    Dave Woestenborghs
    0

    Maybe you can create a custom model called BlogOverview...with one property called BlogPosts...which holds your array...you can create this in your controller and pass it to your view

    Dave

  • Ado 8 posts 77 karma points
    Dec 17, 2015 @ 15:36
    Ado
    0

    Yea so I created a property for blogposts in Blog, and filled it up dynamically with children in the constructor.

    All this is definitely more work, but it allowed me to keep logic out of the view, and create pagination with two simple if statements in the view.

    Will there ever be a problem, if a property I have in my Blog model, doesnt exist as a property in Umbraco Document Type Blog ?

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Dec 17, 2015 @ 15:40
    Dave Woestenborghs
    0

    When working with strongly typed models it's maybe a good idea to have a look at Zbu Models builder or Ditto

  • Samira 113 posts 452 karma points
    Jan 12, 2016 @ 05:01
    Samira
    2

    Hi Ado,

    Firstlly create custom model for fetch data from Umbraco database

    Please try this in razor page for inherit data from custom model

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "Master.cshtml";
    }
    @inherits UmbracoViewPage<yourapplicationname.Models.yourmodelname>
    

    hope this help,

    Regards, Samira

Please Sign in or register to post replies

Write your reply to:

Draft