当值在数组中时选定的MVC 5 Razor html.Drodown listfor集
View
:
@model MyProject.Web.API.Models.AggregationLevelConfViewModel[...]@Html.DropDownListFor(m => m.Configurations[0].HelperCodeType, (SelectList)Model.HelperCodeTypeItems, new { id = "Configurations[0].HelperCodeType" })
ViewModel
public class AggregationLevelConfViewModel{ private readonly List<GenericIdNameType> codeTypes; private readonly List<GenericIdNameType> helperCodeTypes; public IEnumerable<SelectListItem> CodeTypeItems { get { return new SelectList(codeTypes, "Id", "Name"); } } public IEnumerable<SelectListItem> HelperCodeTypeItems { get { return new SelectList(helperCodeTypes, "Id", "Name"); } } public int ProductionOrderId { get; set; } public string ProductionOrderName { get; set; } public IList<Models.AggregationLevelConfiguration> Configurations { get; set; } public AggregationLevelConfViewModel() { // Load CodeTypes to show it as a DropDownList byte[] values = (byte[])Enum.GetValues(typeof(CodeTypes)); codeTypes = new List<GenericIdNameType>(); helperCodeTypes = new List<GenericIdNameType>(); for (int i = 0; i < values.Length; i++) { GenericIdNameType cType = new GenericIdNameType() { Id = values[i].ToString(), Name = EnumHelper.GetDescription((CodeTypes)values[i]) }; if (((CodeTypes)values[i]) != CodeTypes.NotUsed) codeTypes.Add(cType); helperCodeTypes.Add(cType); } }}
new SelectList(codeTypes, "Id", "Name");
new SelectList(helperCodeTypes, "Id", "Name");
Configurations
AggregationLevelConfiguration.CodeType
AggregationLevelConfiguration.HelperCodeType
.
达令说