Converting IPublisedContent to specific model fails
I'm trying to convert a content node from IPublisedContent to a specific Model.
My Model:
public class PersonModel : PageModel
{
public IPublishedContent Portrait { get; set; }
public string PersonName { get; set; }
public string ShortDescription { get; set; }
public string LongDescription { get; set; }
public PersonModel(IPublishedContent content) : base(content)
{
}
}
Converting IPublisedContent:
person.As<PersonModel>()
Usage:
@foreach (var person in Model.Children.Where(x => x.IsVisible()))
{
@Html.Partial("~/Views/.../Sections/_Person.cshtml", person.As<PersonModel>())
}
This works fine, except if no Portrait is chosen. If i try to render the page without selecting a portrait the line: person.As<PersonModel>()) causes the following error:
Object reference not set to an instance of an object.
[No relevant source lines]
It wasn't clear to me that you were using Ditto. The media picker stores it's value as a integer. So it can not convert out of the box to a IPublishedContent.
I solved this with a slightly different approach. Instead of using the Model.Portrait directly in my razor view I created a method for getting the image in the partial view.
public static string GetUrlForImage(IPublishedContent mediaContent, int width = 300, int height = 300)
{
return mediaContent != null ? mediaContent.GetAzureCropUrl(width,height) : "/images/404/404.jpg";
}
This also allows me to set a default "image not found"-image
Converting IPublisedContent to specific model fails
I'm trying to convert a content node from IPublisedContent to a specific Model.
My Model:
Converting IPublisedContent:
Usage:
This works fine, except if no Portrait is chosen. If i try to render the page without selecting a portrait the line: person.As<PersonModel>()) causes the following error:
Hi Johannes,
How do you set the properties of your PersonModel ?
Dave
Hi Dave :)
I'm not sure i understand what you want to know. The properties are set on the content node in the umbraco backoffice
These values are assigned to the PersonModel using the .As<T> method, found in Our.Umbraco.Ditto.PublishedContentExtensions
Apart from this there is no assigning of property values.
Hi Johannes,
It wasn't clear to me that you were using Ditto. The media picker stores it's value as a integer. So it can not convert out of the box to a IPublishedContent.
I have this package installed which handles this : https://our.umbraco.org/projects/developer-tools/umbraco-core-property-value-converters/
But you can probably do it using a type convertor that ships with ditto. You need to add it as attribute of the Portrait property.
Dave
Maybe it can also be done with the built in DittoMediaPickerConverter but I haven't used that one.
Dave
Hi Johannes, Dave,
Use the
DittoPickerConverter
on the class as follows, the other picker converters are marked obsolete.That should then map.
Cheers
James
Hi James
Thanks for the clarification
Dave
Thanks for the replies :)
I solved this with a slightly different approach. Instead of using the Model.Portrait directly in my razor view I created a method for getting the image in the partial view.
This also allows me to set a default "image not found"-image
is working on a reply...