我使用MVC4,mvc4动态dropdownlistfor绑定选定值
在控制器侧我有2个表:
1>CarrierList: - 它包含交通数据列表。
2>TransPortationModeList: - 它包含所选运输数据的ID &它来自数据库。
现在,在编辑模式下,我必须首先将dropdownlist与“CarrierList”记录绑定。 并且还选择该列表的哪个id(值)应该来自“TransPortationModeList”的属性。
对于我的看法代码:
@foreach (var Data in Model.TransPortationModeList)
{
@Html.DropDownListFor(lm => lm.TransPortationModeList[Model.TransPortationModeList.IndexOf(Data)].TransModeId, Model.CarrierList.Select(c => new SelectListItem { Text = c.TransportationName + "-" + c.TransportationModelID, Value = c.TransportationModelID }), TransPortationModeList[Model.TransPortationModeList.IndexOf(Data)].TransModeId, new { @class = "form-control" })
}
这里:TransPortationModeList [Model.TransPortationModeList.IndexOf(数据)] TransModeId这是给我的ID。
now,by this code.i am not bind bind dropdownlist for Selected record。
请让我知道。我在这里错过了什么?
谢谢。
你的'Data'属性是如何初始化的?此外,在您收集“SelectListItem”之后,您不应该指定'TransModeId',因为所选值应该由第一个参数提供('lm => lm.TransPortationModeList [Model.TransPortationModeList.IndexOf(Data)]。TransModeId ') –
@RédaMattar - 用于我正在使用foreach的数据。让我再次更新我的问题。 – Kvadiyatar