Hi there! This blog post was published over 18 months ago. A lot can change in that time, so some of the information might now be outdated. It’s worth verifying the details or checking for newer updates.
I thought I would share the solution to getting the pre-values from a Data Type, as the documentation for this on the Umbraco website seems to have been removed.
The code below is an edited version of what worked for me (to remove the project details).
You will need to replace NAME or name with your values to make this work correctly, however, it should point in the right direction.
Controller
[ChildActionOnly]
[ActionName("NAME")]
public ActionResult NAME()
{
return PartialView("~/Views/Partials/NAME.cshtml", new MODELNAME{NAME = GetSelectListForNAME()});
}
private SelectList GetSelectListForNAME()
{
var preValueDataType = Umbraco.DataTypeService.GetPreValuesCollectionByDataTypeId(1301); //Change Data Type ID
var preValues = preValueDataType.PreValuesAsDictionary.Values.Where(pdv => pdv.Value != "0");
var namesList = preValues.Select(preValue => new SelectListItem
{
Value = preValue.Id.ToString(),
Text = preValue.Value
})
.ToList();
return new SelectList(namesList);
}
Model
public SelectList NAMES { get; set; }
public string NAME { get; set; }
Razor View
This will populate a drop-down list
@Html.DropDownListFor(model => model.NAME, Model.NAMES.Items as List)
About the author
Aaron Sadler
Aaron Sadler, Umbraco MVP (2x), Umbraco Certified Master Developer and DevOps Engineer