2

私はMVC4を使用しています、

コントローラー側には2つのリストがあります:

1> CarrierList :- 輸送データのリストが含まれています。

2> TransPortationModeList :- 選択した輸送データの ID が含まれており、データベースから取得されています。

さて、編集モードでは、最初にドロップダウンリストを「CarrierList」レコードにバインドする必要があります。また、「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 に答える