Skip to content

Instantly share code, notes, and snippets.

@ryanlewis
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanlewis/9eb27975d690b5f12e6c to your computer and use it in GitHub Desktop.
Save ryanlewis/9eb27975d690b5f12e6c to your computer and use it in GitHub Desktop.
Property Value Converter for a array of strings
using System;
using Newtonsoft.Json;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
namespace Epiphany.PropertyEditorValueConverters
{
[PropertyValueType(typeof(string[]))]
[PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
public class StringArrayValueConverter : PropertyValueConverterBase
{
public override bool IsConverter(PublishedPropertyType propertyType)
{
return propertyType.PropertyEditorAlias.Equals("Epiphany.SortableStringList");
}
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
if (source == null) return null;
var sourceString = source.ToString();
if (String.IsNullOrWhiteSpace(sourceString)) return null;
try
{
return JsonConvert.DeserializeObject<string[]>(sourceString);
}
catch
{
return null;
}
}
}
}
@ryanlewis
Copy link
Author

Usage:

<ul>
    @foreach (var s in Model.GetPropertyValue<string[]>("stringList"))
    {
        <li>@s</li>
    }
</ul>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment