我正在使用C#和.NET Framework 4.6.1开发ASP.NET MVC 5应用程序。
我有这个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 codeTypes;
private readonly List helperCodeTypes;
public IEnumerable CodeTypeItems
{
get { return new SelectList(codeTypes, "Id", "Name"); }
}
public IEnumerable HelperCodeTypeItems
{
get { return new SelectList(helperCodeTypes, "Id", "Name"); }
}
public int ProductionOrderId { get; set; }
public string ProductionOrderName { get; set; }
public IList Configurations { get; set; }
public AggregationLevelConfViewModel()
{
// Load CodeTypes to show it as a DropDownList
byte[] values = (byte[])Enum.GetValues(typeof(CodeTypes));
codeTypes = new List();
helperCodeTypes = new List();
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 CodeTypeItems
{
get { return new SelectList(codeTypes, "Id", "Name"); }
}
public IEnumerable HelperCodeTypeItems
{
get { return new SelectList(helperCodeTypes, "Id", "Name"); }
}
但是我无法设置它,new SelectList(codeTypes, "Id", "Name");或者new
SelectList(helperCodeTypes, "Id", "Name");因为选择的值在Configurations数组中:fields
AggregationLevelConfiguration.CodeType和AggregationLevelConfiguration.HelperCodeType。
我想我必须在“视图”中设置选定的值,但是我不知道该怎么做。
如何设置选择的值?