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.
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:
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.
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.
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
}
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.
Hi,
Here's two ways of doing it.
Regards
David
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:
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.
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.
Then check the alias. Something like this but guessing the correct syntax
Then call your partial like this.
Hope this helps.
Regards
David
is working on a reply...