Copied to clipboard

Flag this post as spam?

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


  • Johannes Giske 8 posts 121 karma points
    Jan 20, 2016 @ 11:28
    Johannes Giske
    0

    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]

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 20, 2016 @ 11:41
    Dave Woestenborghs
    0

    Hi Johannes,

    How do you set the properties of your PersonModel ?

    Dave

  • Johannes Giske 8 posts 121 karma points
    Jan 20, 2016 @ 11:53
    Johannes Giske
    0

    Hi Dave :)

    I'm not sure i understand what you want to know. The properties are set on the content node in the umbraco backofficeenter image description here

    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.

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 20, 2016 @ 12:00
    Dave Woestenborghs
    0

    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

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 20, 2016 @ 13:03
    Dave Woestenborghs
    0

    Maybe it can also be done with the built in DittoMediaPickerConverter but I haven't used that one.

    Dave

  • James Jackson-South 489 posts 1747 karma points c-trib
    Jan 20, 2016 @ 13:17
    James Jackson-South
    1

    Hi Johannes, Dave,

    Use the DittoPickerConverter on the class as follows, the other picker converters are marked obsolete.

    [TypeConverter(typeof(DittoPickerConverter))]
    public PersonModel(IPublishedContent content) : base(content)
    {
    }
    

    That should then map.

    Cheers

    James

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 20, 2016 @ 14:22
    Dave Woestenborghs
    0

    Hi James

    Thanks for the clarification

    Dave

  • Johannes Giske 8 posts 121 karma points
    Jan 21, 2016 @ 08:29
    Johannes Giske
    0

    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.

    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

Please Sign in or register to post replies

Write your reply to:

Draft