Copied to clipboard

Flag this post as spam?

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


  • MB 113 posts 422 karma points
    Aug 04, 2020 @ 06:32
    MB
    0

    Casting to PublishedContentModel from ModelTypeAlias

    I seem to have gotten myself into one of those mental blocks.

    I have a situation where I am in a PartialView and have a ViewData["itemName"] that I need to cast into a PublishedContentModel to use strong typing for getting specific property values, but it's possible it could be one of a few models and I only have the ModelTypeAlias string to identify what is cast is required.

    Every way I look at it I get a conflict of cast conversions between the various potential types.

    I'd welcome someone pointing me to the obvious.

  • David Armitage 508 posts 2078 karma points
    Aug 04, 2020 @ 11:46
    David Armitage
    0

    Hi,

    Here's two ways of doing it.

    var test1 = (YourDocTypeHere)Model.Content;
    
    var test2 = Model.Content as YourDocTypeHere;
    

    Regards

    David

  • MB 113 posts 422 karma points
    Aug 04, 2020 @ 12:30
    MB
    0

    Thanks but I probably didn't explain it well enough.

    I needed to pass a related node as a ViewData item to a PartialView - i.e. it's not the PartialView's Model.

    Anything in ViewData needs to be cast back to its type, so I also passed the node's DocumentTypeAlias, so I know what type it should be cast as when I get there.

    Problem is that the node can be one of a few types, so conceptually, what I need to do is:

    var item = node-alias == MyDocType1.ModelTypeAlias ? (MyDocType1) viewdata['node'] : (MyDocType2) viewdata['node'];
    

    i.e. If the passed node-type == MyDocType1.ModelTypeAlias then cast the node-data as MyDocType1 - Else cast the node-data as MyDocType2

    However, that's a no-go as the compiler then can't determine the implicit type to assign the item with, and barks about it.

    And I haven't been smart enough to figure out a generic converter to manage it.

    Obviously, I can resort to using IPublishedContent and to the tedious (error-prone) HasValue() GetPropertyValue<>() etc - but that rather defeats the purpose of strong typing.

  • David Armitage 508 posts 2078 karma points
    Aug 04, 2020 @ 12:59
    David Armitage
    0

    Hi,

    I think the issue might be saving the whole node into ViewData. I have never tried that. I think something would break doing this.

    In this page I would pass the generic IPublishedContent directly into the partial view. So this means it can accept any type of content.

    You can then run some if statements based on the contentType alias of the IPublishedContent you have passed in and then cast it accordingly form there.

    I do a similar thing all the time.

    So you partial should look something like this at the top.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
    

    Then check the alias. Something like this but guessing the correct syntax

    if(Model.ContentType.ContentTypeAlias == "oneTypeHere")
    {
         //do you cast here
    }
    else if(Model.ContentType.ContentTypeAlias == "theOtherTypeHere")
    {
         //do you cast here
    }
    

    Then call your partial like this.

    @Html.Partial("~/views/partials/TestPartial.cshtml", YourIPublishedContentHere)
    

    Hope this helps.

    Regards

    David

Please Sign in or register to post replies

Write your reply to:

Draft