Aaron Sadler
Posted by:

Aaron Sadler

Category

Umbraco

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.

[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)
Comments

Post a comment

Fields marked with an * (asterisk) are required


Recent Posts

News
.NET 9 Now Available

DotNet 9 Has been rolled ou...

News
Black Friday 2024 - FREE Umbraco Forms licence!

How to claim your FREE Umbr...

Umbraco
How to deploy Umbraco using Web Deploy from Azu...

How to deploy Umbraco using...

Umbraco
How to deploy Umbraco using Web Deploy from Git...

This blog post shows how yo...

Umbraco
Umbraco Medium Security Vulnerabilities - 22nd...

Security patches now availa...

ADVERTISTING
(Umbraco Hosting)

Umbraco Hosting Starting At €23/month