我有这个观点:
@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,"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); } } }
而Models.AggregationLevelConfiguration是:
public class AggregationLevelConfiguration { public byte AggregationLevelConfigurationId { get; set; } public int ProductionOrderId { get; set; } public string Name { get; set; } public byte CodeType { get; set; } public byte HelperCodeType { get; set; } public int PkgRatio { get; set; } public int RemainingCodes { get; set; } }
我需要在这些属性中设置选定的值:
public IEnumerable<SelectListItem> CodeTypeItems { get { return new SelectList(codeTypes,"Name"); } } public IEnumerable<SelectListItem> HelperCodeTypeItems { get { return new SelectList(helperCodeTypes,"Name"); } }
但我无法在新的SelectList中设置它(codeTypes,“Id”,“Name”);或新的SelectList(helperCodeTypes,“Name”);因为所选值位于Configurations数组中:fields AggregationLevelConfiguration.CodeType和AggregationLevelConfiguration.HelperCodeType.
我想我必须在View中设置选定的值,但我不知道该怎么做.
如何设置所选值?
解决方法
解决此问题的2选项是为了确保根据model属性选择正确的选项
选项1(使用EditorTemplate)
为集合中的类型创建自定义EditorTemplate.在/Views/Shared/EditorTemplates/AggregationLevelConfiguration.cshtml中创建一个部分(注意名称必须与该类型的名称匹配)
@model yourAssembly.AggregationLevelConfiguration @Html.DropDownListFor(m => m.HelperCodeType,(SelectList)ViewData["CodeTypeItems"]) .... // other properties of AggregationLevelConfiguration
然后在主视图中,将SelectList作为additionalViewData传递给EditorTemplate
@using (Html.BeginForm()) { ... @Html.EditorFor(m => m.Configurations,new { CodeTypeItems = Model.CodeTypeItems }) ...
选项2(在每次迭代中生成一个新的SelectList并设置selectedValue)
在此选项中,您的属性CodeTypeItems应该是IEnumerable< GenericIdNameType>,而不是SelectList(或者只是使codeType成为公共属性).然后在主视图中
@Html.DropDownListFor(m => m.Configurations[0].HelperCodeType,new SelectList(Model.CodeTypeItems,"Name",Model.Configurations[0].HelperCodeType)
附注:不需要使用新的{id =“Configurations [0] .HelperCodeType” – 已经生成了该属性的DropDownListFor()方法