Skip to content

Property Value Converter

Hendy Racher edited this page Jul 6, 2014 · 6 revisions

Rendering nuPickers with Razor

All nuPicker property editors return a "Picker" object. Picker has a property "PickedKeys" which is a IEnumerable<string> this collection will always contain the raw keys (id's etc) of the picked items.

For all of the typed models you will need to have a reference to nuPickers.PropertyValueConverters.

e.g. @using nuPickers.PropertyValueConverters

Rendering the nuPicker Raw Keys

Strongly Typed IPublishedContent Model Example

@{
    var pickedItems = Model.Content.GetPropertyValue<Picker>("myNuPickerPropertyAlias");
    <ul>
        @foreach (var item in pickedItems.PickedKeys)
        {
            <li>@item</li>
        }
    </ul>
}

DynamicPublishedContent Model Example

@{
    var dynamicPickedItems = CurrentPage.myNuPickerPropertyAlias;
    <ul>
        @foreach (var item in dynamicPickedItems.PickedKeys)
        {
            <li>@item</li>
        }
    </ul>
}

Helper Methods

There are a number of helper methods within the "Picker" to make rendering easy.

AsPublishedContent and AsDynamicPublishedContent

If your picked keys are either Umbraco Content, Media or Members then these methods will return them as IPublishedContent or DynamicPublishedContent, the method you need to use depends on if you are using strongly typed (Model.Content) or dynamic (CurrentPage) Umbraco models.

Strongly Typed IPublishedContent Model Example

@{
    var pickedItems = Model.Content.GetPropertyValue<Picker>("myNuPickerPropertyAlias");
    <ul>
        @foreach (var item in pickedItems.AsPublishedContent())
        {
            <li>@item.Name - @item.GetPropertyValue("title")</li>
        }
    </ul>
}

DynamicPublishedContent Model Example

@{
    var dynamicPickedItems = CurrentPage.myNuPickerPropertyAlias;
    <ul>
        @foreach (var item in dynamicPickedItems.AsDynamicPublishedContent())
        {
            <li>@item.Name - @item.title</li>
        }
    </ul>
}

AsEnum<T> and AsEnum

If you are using the Enum Pickers then you can use these methods to easily be returned the Enum that was selected

Strongly Typed IPublishedContent Model Example

@{
    var pickedEnum = Model.Content.GetPropertyValue<Picker>("enumCheckboxPicker");
    <ul>
        @foreach (var myEnum in pickedEnum.AsEnum<MyEnumType>())
        {
            <li>@myEnum</li>
        }
    </ul>
}

DynamicPublishedContent Model Example

@{
    var pickedEnum = CurrentPage.enumCheckboxPicker;
    <ul>
        @foreach (var myEnum in pickedEnum.AsEnum())
        {
            <li>@myEnum</li>
        }
    </ul>
}
Clone this wiki locally