Copied to clipboard

Flag this post as spam?

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


  • Rene M. Macalisang 6 posts 75 karma points
    Mar 09, 2024 @ 16:25
    Rene M. Macalisang
    0

    IPublishedContent parse to Custom View Model using umbraco 13

    Hello, I have problem with my umbraco 13, I want to convert the IpublisedContent to strongly type custom view model.

    Example I have Title and Remarks properties in my Home Page, I want to parse this to strongly type custom view model.

    Here is my sample custom view model.

     public class HomePage
     {
         public string Title { get; set; }
         public string Remarks { get; set; }
     }
    

    This is the backoffice.enter image description here

    Please help.

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Mar 12, 2024 @ 22:44
    Marc Goodson
    0

    Hi Rene

    First thought is have you considered looking at Modelsbuilder at all...

    https://docs.umbraco.com/umbraco-cms/reference/templating/modelsbuilder/introduction

    When it's 'turned on' it will generate strongly typed c# models for each Document Type in Umbraco.

    These generated models all implement IPublishedContent so you can cast easily into them, or as they are generated as partial c# classes you can extend them etc... Anyway worth having a look at the first...

    If you prefer not to use Modelsbuilder then you'd need to handle mapping the properties yourself., eg

    var homepage = new HomePage() {Title = content. Value<string>("title"), Remarks = content.Value<string>("remarks")};

    Where content is your IPublishedContent object.

    You could make some kind of service or mapper, if the same mappings happen in more than one place.

    But check out Modelsbuilder as it takes a lot of the pain out of this kind of work.

    Regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft