Bartosz Sekula
Nov 30, 2017
  14733
(12 votes)

Property Value List

We introduced the List<T> property some time ago, which allows you to create properties that accept POCO objects from editors.

The feedback has been really positive, but there were suggestions that it should also be possible to provide the same concept but for simpler data types.

The editors would then be able to input data directly on the page, without the need to use a dialog or to specify a container data type.

Introduction

With CMS.UI 11.1.0, we are introducing a brand new Property Value List that allows the editors to input multiple primitive values.

Available types are:

  • String
  • Int
  • DateTime
  • Double

The idea is the same as with the List<T>, you can just add the list as a new property to your model:

Default descriptors match the IList type and render the correct editor.

public virtual IList<string> ListOfStrings { get; set; }

public virtual IList<int> ListOfIntegers { get; set; }

public virtual IList<DateTime> ListOfDates { get; set; }

public virtual IList<double> ListOfDoubles { get; set; }

Please note that the property type does not have to be IList. Our descriptors are configured to match the IList interface but they also cover all the base ones so if you prefer to use IEnumerable or ICollection, that is totally up to you. All the following declarations are going to be matched by the Value List descriptor.

public virtual IEnumerable<string> EnumerableStrings { get; set; }

public virtual ICollection<string> CollectionOfStrings { get; set; }

public virtual IList<string> ListOfStrings { get; set; }

Validation

It is possible to apply validation to both the list itself and to the individual items.

You can use ListItemsAttribute in order to control the number of items in the list:

[ListItems(5)]
public virtual IList<int> Max5Items { get; set; }

The attributes that can be applied to individual items are:

[ItemRangeAttribute(1, 10)]
public virtual IList<int> ItemsBetween1And10 { get; set; }

[ItemRegularExpression("[a-zA-Z]*")]
public virtual IList<string> LettersOnly { get; set; }

[ItemStringLength(3)]
public virtual IList<string> ListOfAcronyms { get; set; }

Rendering

Let's say you have an IList<string> property that you would like editors to be able to edit from on-page edit view.

The property is defined as following:

[Required]
[Display(Order = 305)]
[CultureSpecific]
public virtual IList<string> UniqueSellingPoints { get; set; }

Without a specific Display Template you will not be able to use the PropertyFor helper method.

In order to achieve that you have to attach a UIHint to your property:

[UIHint("StringsCollection")]
public virtual IList<string> UniqueSellingPoints { get; set; }

Then add a StringsCollection.cshtml file to ~/Views/Shared/DisplayTemplates folder.

An Example Display Template can be seen in the latest Alloy package:

@model IEnumerable<string>
@if(Model != null && Model.Any())
{
    <ul>
        @foreach(var stringValue in Model)
        {
            <li>@stringValue</li>
        }
    </ul>
}

After adding a UniqueSellingPoints property to your page template:

@Html.PropertyFor(x => x.CurrentPage.UniqueSellingPoints)

You will see the following result in on-page edit view:

Nov 30, 2017

Comments

Dec 1, 2017 02:32 AM

Thanks to Mark Hall for the tip about this article. This is just what I needed to fix the problem with string[] MetaKeywords (from alloy demo) not opening properly.

Just as soon as I can upgrade to EPI 11 I will be grateful for this in many ways!

Currently I have dependancy issues with multiple packages.

Cheers!

Dec 4, 2017 03:46 AM

Great work!

This will surely be useful in a lot of projects.

Björn Olsson
Björn Olsson Dec 14, 2017 01:49 PM

I would be great if this could be combined with the [SelectMany] attribute. Instead of just using a comma separated string (which is the only option today according to my knowledge).

Mar 13, 2018 02:08 PM

When using the valiadtion attribute to limit the number of items to 5, I should not be allowed to add more than 5 items. 

[ListItems(5)]

It would be more clear to editors if the + (add another item button) are hidden, insted of displaying the message "field is illegal"...

Arash Masoudnia
Arash Masoudnia Jul 12, 2018 06:02 PM

You can also put the view in these addresses for these 2 useful Html helper method:

For propertyfor:
    ~/Views/Shared/DisplayTemplates

    ~/Views/Controller_Name/DisplayTemplates
 For DisplayFor: 

~/Views/Shared/DisplayTemplates

~/Views/Controller_Name/DisplayTemplates


For example:

@Html.DisplayFor(x => x.CurrentPage.UniqueSellingPoints)

@Html.PropertyFor(x => x.CurrentPage.UniqueSellingPoints)

Patrik Nordin
Patrik Nordin Oct 31, 2018 11:36 AM

Great advice! Will fit in nicely with MetaKeywords.

Deepa Puranik
Deepa Puranik Mar 2, 2022 11:51 AM

Hi,

Using ListItemAttribute to restrict number of items we don't have check for empty value that's been added?

Thanks,

Deepa

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024