2

我正在使用 MVC4 ,

在控制器端,我有 2 个列表:

1> CarrierList :- 它包含运输数据列表。

2> TransPortationModeList :- 它包含所选交通数据的 id 并且它来自数据库。

现在,在编辑模式下,我必须将下拉列表与“CarrierList”记录绑定的第一件事。并选择该列表的属性,其中 id(value) 应该来自“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(Data)].TransModeId 它为我提供了 ID。

现在,通过此代码。我无法为选定记录绑定下拉列表。

请告诉我。我在这里想念什么?

谢谢。

4

1 回答 1

-1

使用循环可以不那么冗长for,因为您避免IndexOf使用:

@for(int index = 0; index < Model.TransPortationModeList.Count; index++)
{
    @Html.DropDownListFor(lm => lm.TransPortationModeList[index].TransModeId,
                                Model.CarrierList.Select(c => new SelectListItem { Text = c.TransportationName + "-" + c.TransportationModelID, Value = c.TransportationModelID }), 
                                new { @class = "form-control" })

}

第一个参数将包含选定的值。

于 2013-10-31T10:17:44.107 回答