Add methods to access specific properties from a Fieldset
* GetValue("alias") - gets the value as a string
* GetValue<T>("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.
This commit is contained in:
@@ -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<Property>();
|
||||
}
|
||||
|
||||
public string GetValue(string propertyAlias)
|
||||
{
|
||||
return GetValue<string>(propertyAlias);
|
||||
}
|
||||
|
||||
public T GetValue<T>(string propertyAlias)
|
||||
{
|
||||
var property = GetProperty(propertyAlias);
|
||||
|
||||
if (property == null || string.IsNullOrEmpty(property.Value))
|
||||
return default(T);
|
||||
|
||||
var convertAttempt = property.Value.TryConvertTo<T>();
|
||||
|
||||
if (convertAttempt.Success)
|
||||
return convertAttempt.Result;
|
||||
|
||||
return default(T);
|
||||
}
|
||||
|
||||
private Property GetProperty(string propertyAlias)
|
||||
{
|
||||
return Properties.FirstOrDefault(p => p.Alias.InvariantEquals(propertyAlias));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user