Copied to clipboard

Flag this post as spam?

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


  • Newbraco 41 posts 164 karma points
    Apr 18, 2018 @ 16:18
    Newbraco
    0

    Basic question:

    Am I correct saying that you dont use a ViewModel in umbraco pages, but solely use data from the Document Type?

    In our project we have some setup-data (callbackurls, title for that control, id a s o) that should be set on each partial. We would prefer it to be deployed with the solution. This is nothing that should be changed or set by our Backoffice staff.

    In a non-Umbraco solution I would have set these props (except title) in the initiation of the VM. So far I have created some Partials using

    @Html.Partial("_MyPartial", new MyPartialVM{hardcoded props..});
    @Html.Partial("_MyPartial", new MyPartialVM{other hardcoded props..});
    

    This kind of works, but I wonder if Im doing it in the Umbraco spirit.

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Apr 18, 2018 @ 16:45
    Nik
    2

    Hi Newbraco,

    It's not really the case. You can use your own custom view models in partial views but it does depend a little on what you are trying to do.

    Partial views should inherit from UmbracoViewPage. Now what's good about this is that UmbracoViewPage has a generic override where you can also specify the model.

    So you can easily do something like this in your view model:

    @inherits UmbracoViewPage<MyCustomModel>
    

    Then you can pass this instance in to your call to the partial in the same way that you have done in other MVC sites.

    When doing this in the view @Model will be your custom model. If you need to access the Umbraco Page that called it you have access to the @Umbraco, which is the Umbraco helper, so you should be able to do:

     @Umbraco.AssignedContent
    

    The caution with that line though, is it is possible that AssignedContent could not be set so just watch out for that.

    Nik

  • Newbraco 41 posts 164 karma points
    Apr 19, 2018 @ 08:33
    Newbraco
    0

    Hi Nik Thanks for your answer! This is the Umbraco-spirited answer I was looking for :)

    When can @Umbraco.AssignedContent be null/not set? Isnt the partials always in a Umbraco page?

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Apr 19, 2018 @ 08:54
    Nik
    1

    Honestly, I don't know. Someone else warned me about it but I've never come across it. I guess if you are using custom routing and route hijacking with custom models at that point you enter the realms of possible Null values for Umbraco.AssignedContent but I'm just guessing.

    Happy to help :-)

    Nik

  • Newbraco 41 posts 164 karma points
    Apr 19, 2018 @ 10:01
    Newbraco
    0

    Ok, good to know at least.

    Thanks again! :)

Please Sign in or register to post replies

Write your reply to:

Draft