Umbraco 7.6 Property Value Converters (Breaking Change)

property value converters contributed by the community have been merged into the core

Posted on Wednesday, May 31, 2017


May 2017 saw the launch of v7.6 of the Umbraco CMS. There are a number of quality changes and on the umbraco blog it is stated that  "Umbraco 7.6 is a major minor upgrade.

The are a number of breaking changes and I recommend catching up with them on the release page:

This blog article covers an area that if ignored will cause a lot of razor errors as the code used in the past may not work!

Property Value Converters

A large amount of property value converters contributed by the community have been merged in and are now the default value converters. These converters change the object types returned by GetPropertyValue for more convenient types.

This change only impacts new installs (no change for upgrades). In ugrades i believe the EnablePropertyValueConverters is set to false in umbracoSettings.config.

 

!! In umbracoSettings.config leave EnablePropertyValueConverters set to false - this will help your existing content queries to still work.

 
In this example will look at the built in property editor : Dropdown list multiple.  
(https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/DropDown-List-Multiple)

Pre 7.6

Returns: Comma Separated String

Typed MVC razor code:

@{
  if (Model.Content.HasValue("fruit")){                                                     
       <ul>                                                        
      @foreach(var item in Model.Content.GetPropertyValue<string>("fruit").Split(',')) { 
        <li>@item</li>
      }
    </ul>                                                                                       
  }
}

After version 7.6+ ,  if you set the following setting to false, the above code will work as expected.

<!-- Enables value converters for all built in property editors so that they return strongly typed object, recommended for use with Models Builder -->
<EnablePropertyValueConverters>false</EnablePropertyValueConverters>

However, if you set this to true, you will need to use the code below as this property editor will return an string array.

 

@{
  if (Model.Content.HasValue("fruit")){                                                     
       <ul> 
IEnumerable<string> items = Model.Content.GetPropertyValue< IEnumerable<string>>("fruit"); @foreach(var item in items) { <li>@item</li> } </ul> } }

 

If you want to get you the raw value you can use the following code:

Model.Content.GetProperty("fruit").DataValue 

 

References: