当值在数组中时选定的MVC 5 Razor html.Drodown listfor集我正在用C#和.NET Framework4.6.1开发ASP.NETMVC 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<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.我想我必须在视图中设置选定的值,但是我不知道如何去做。如何设置选定的值?
3 回答
达令说
TA贡献1821条经验 获得超6个赞
@Html.DropDownListFor()
备选案文1EditorTemplate
)
EditorTemplate
/Views/Shared/EditorTemplates/AggregationLevelConfiguration.cshtml
@model yourAssembly.AggregationLevelConfiguration@Html.DropDownListFor(m => m.HelperCodeType, (SelectList)ViewData["CodeTypeItems"]).... // other properties of AggregationLevelConfiguration
SelectList
EditorTemplate
additionalViewData
@using (Html.BeginForm()){ ... @Html.EditorFor(m => m.Configurations , new { CodeTypeItems = Model.CodeTypeItems }) ...
备选案文2SelectList
selectedValue
)
CodeTypeItems
IEnumerable<GenericIdNameType>
SelectList
codeTypes
@Html.DropDownListFor(m => m.Configurations[0].HelperCodeType, new SelectList(Model.CodeTypeItems, "Id", "Name", Model.Configurations[0].HelperCodeType)
new { id = "Configurations[0].HelperCodeType"
DropDownListFor()
id
- 3 回答
- 0 关注
- 526 浏览
添加回答
举报
0/150
提交
取消