From e5f8dc56347bcb1014de80e3600d314bdc958ffd Mon Sep 17 00:00:00 2001 From: Tom Fulton Date: Wed, 15 Jan 2014 15:52:39 -0700 Subject: [PATCH] Add methods to access specific properties from a Fieldset * GetValue("alias") - gets the value as a string * GetValue("alias") - attempts to convert to the specified types using some of Umbraco's built in TypeConverter logic. If conversion fails, returns default(T). Note this doesn't utilize PropertyValueConverters (yet?) * GetProperty("alias") - gets a reference to the entire property object. Leaving this private for now, but there may be a reason to publicize someday if we add more properties to the Property object. --- .../Umbraco.Archetype/Models/Fieldset.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs b/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs index 72d782f..e2aeaee 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/Fieldset.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Umbraco.Core; namespace Archetype.Umbraco.Models { @@ -16,5 +17,30 @@ namespace Archetype.Umbraco.Models Properties = new List(); } + public string GetValue(string propertyAlias) + { + return GetValue(propertyAlias); + } + + public T GetValue(string propertyAlias) + { + var property = GetProperty(propertyAlias); + + if (property == null || string.IsNullOrEmpty(property.Value)) + return default(T); + + var convertAttempt = property.Value.TryConvertTo(); + + if (convertAttempt.Success) + return convertAttempt.Result; + + return default(T); + } + + private Property GetProperty(string propertyAlias) + { + return Properties.FirstOrDefault(p => p.Alias.InvariantEquals(propertyAlias)); + } + } }