Copied to clipboard

Flag this post as spam?

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


  • David Zweben 215 posts 621 karma points
    Apr 11, 2019 @ 15:48
    David Zweben
    0

    Pass Models Builder model to a partial view?

    I have a partial view which is being used on pages with multiple document types. I'm looking for a way to use Models Builder syntax inside this partial view, perhaps by somehow passing the model type into it.

    I know how to pass the model itself into the Partial View, but on the Partial View end, the model is always of type IPublishedContent, rather than the current doctype's Models Builder type.

    Is there some way to do this, or do I have to use the .Value() syntax to access page data if the document type varies?

  • Nik 1144 posts 4732 karma points MVP 2x c-trib
    Apr 11, 2019 @ 15:52
    Nik
    0

    If you are always passing the same type of model every time then you can change the inherits statement from:

    @inherits UmbracoViewPage
    

    to

    @inherits UmbracoViewPage<ModelType>
    

    This would make your view expect the model type you've specified :-)

    Nik

  • David Zweben 215 posts 621 karma points
    Apr 11, 2019 @ 15:54
    David Zweben
    0

    That's the thing, I am not passing the same type of model every time. Is there a way to pass the type of the model into the partial view, so that the inherited model type can vary?

  • Nik 1144 posts 4732 karma points MVP 2x c-trib
    Apr 11, 2019 @ 16:05
    Nik
    0

    Not that I know of. In that scenario, that is why it defaults back to IPublishedContent as all the models are of that type.

    Do they have common properties between the models, and if so were those properties added via a composition. If they were then you could have a model type which is the interface created by the composition.

    For example,

    Composition type - SEO - Has PageTitle and MetaDescription properties.

    All doc types have SEO as part of their composition, so the models all inherit from ISeo

    In this situation you could do

    @inherits UmbracoViewPage<ISeo>
    

    And then things like

    @Model.PageTitle
    

    would potentially exist.

  • David Zweben 215 posts 621 karma points
    Apr 11, 2019 @ 16:09
    David Zweben
    0

    Thanks! I tried to do something similar by using the type of the doctypes' parent, which didn't work, but doing it with compositions makes sense. I'll give that a try!

  • Nik 1144 posts 4732 karma points MVP 2x c-trib
    Apr 11, 2019 @ 16:12
    Nik
    0

    No problem, the other option you have is create your own model class and specify that as the models type. You'd have to populate the model before each call though :-)

    Nik

Please Sign in or register to post replies

Write your reply to:

Draft