This article is show how to get the prevalue of a editor dynamically
Input the prevalue thru Umbraco.
Result
Method:
Environment :
Umbraco version 7.6.3 assembly: 1.0.6361.21154
Visual Studio 2015
Controller :
public ActionResult RenderCategoryList()
{
Dictionary<int, object> categoryList = new Dictionary<int, object>();
int? typeId = Library.getPropertyIdByName("Your Editior Name");
if (typeId != null)
{
categoryList = Library.GetPrevalues(typeId.Value);
}
return PartialView(PartialViewPath("_SearchCategory"));
}
Library Class
using System.Xml.XPath; using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.Services;
namespace yourNamespace
{
public static class Library
{
public static int? getPropertyIdByName(string name)
{
IDataTypeService service = ApplicationContext.Current.Services.DataTypeService;
IDataTypeDefinition definition = service.GetDataTypeDefinitionByName(name);
return definition != null ? (int?)definition.Id : null;
}
public static Dictionary<int, object> GetPrevalues(int dataTypeId)
{
XPathNodeIterator preValueRootElementIterator = umbraco.library.GetPreValues(dataTypeId);
preValueRootElementIterator.MoveNext(); //move to first
XPathNodeIterator preValueIterator = preValueRootElementIterator.Current.SelectChildren("preValue", "");
var retVal = new Dictionary<int, object>();
while (preValueIterator.MoveNext())
{
retVal.Add(Convert.ToInt32(preValueIterator.Current.GetAttribute("id", "")), preValueIterator.Current.Value);
}
return retVal;
}
}
}
Partial View
@inherits Umbraco.Web.Mvc.UmbracoViewPage<List<Object>>
@using yourNamespace.Models
@*TODO: Render Category from CMS content dynamic*@
<select class="form-control" name="Category">
@foreach (var item in Model)
{
<option class="form-control" label="@item" value="@item" />
}
</select>



沒有留言:
張貼留言