Skip to content
Chriztian Steinmeier edited this page Oct 25, 2017 · 3 revisions

All property editors can return a Picker object as their model. This can be done using the built-in Umbraco PropertyValueConverter, or via the public constructor (supplying a content, media or member id together with a property alias):

@using nuPickers;

var picker = new Picker(1000, "examplePicker");

There is also an extension method available on IPublishedContent:

@using nuPickers;
@using nuPickers.Extensions;

var content new UmbracoHelper(UmbracoContext.Current).TypedContent(1000);        

var picker = content.GetPicker("examplePicker);

Rendering the nuPicker Raw Keys

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

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>
}

AsEnums<T> and AsEnums

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 pickedEnums = Model.Content.GetPropertyValue<Picker>("enumCheckboxPicker");
    <ul>
        @foreach (var myEnum in pickedEnums.AsEnums<MyEnumType>())
        {
            <li>@myEnum</li>
        }
    </ul>
}

DynamicPublishedContent Model Example

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