Copied to clipboard

Flag this post as spam?

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


  • Meni 281 posts 539 karma points
    Oct 24, 2022 @ 17:39
    Meni
    0

    Getting errors while try to convert code from dynamic to strongly typed

    Hi, I try to convert my code from dynamic to strongly typed. I'm using V7 and in my web.config I have:

    <add key="Umbraco.ModelsBuilder.Enable" value="true" />
    <add key="Umbraco.ModelsBuilder.ModelsMode" value="PureLive" />
    

    (I also verified it appears on the developer section of the back office)

    In my partial view code (Partial View Macro Files), I tried to change as follow, from:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{  
       string myWebsiteMagazineCategories = CurrentPage.myWebsiteMagazineCategory; //get the checked item from the checkbox list
    

    To:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{  
       string myWebsiteMagazineCategories = Model.Content.myWebsiteMagazineCategory; //get the checked item from the checkbox list
    

    And from:

    var magazine_node = Umbraco.Content(MagazineID);
    

    To:

    var magazine_node = Umbraco.TypedContent(MagazineID);
    

    But now I get the following error:

    Compiler Error Message: CS1061: 'IPublishedContent' does not contain a definition for 'myWebsiteMagazineCategory' and no extension method 'myWebsiteMagazineCategory' accepting a first argument of type 'IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
    

    Any idea?

    Thanks a lot !

  • Marc Goodson 2157 posts 14435 karma points MVP 10x c-trib
    Oct 27, 2022 @ 19:08
    Marc Goodson
    0

    HI Meni

    In a Partial View Macro then the underlying model is of type PartialViewMacroModel

    A Macro can be placed on any page

    So it won't be specifically 'typed' to a specific type of Modelsbuilder generated Model, as these match to DocumentTypes.

    Model.Content does represent a strongly typed IPublishedContent object representing the CurrentPage the macro is on, but without strongly typed access to properties:

    So you can access properties generically via:

    string myWebsiteMagazineCategories = Model.Content.GetPropertyValue<string>("myWebsiteMagazineCategory");
    

    If you knew for sure your macro was only ever on a NewsArticle page then you could cast the Model.Content item to that particular generated modelsbuilder type eg

    var currentNewsArticle = Model.Content.As<NewsArticle>();
    

    and then you would have access to the strongly type properties generated by modelsbuilder and be able to access them via

    currentNewsArticle.MyWebsiteMagazineCategories;
    

    if that helps!

    regards

    Marc

  • 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