我已经阅读了这个问题ASP.NET MVC DropDownListFor not selection value from model and answer,但我不知道我的问题的解决方案。
这是我的看法:
@foreach (var adjusterLanguages in Model.adjusterLanguages)
{
<div class="editor-field row">
@Html.DropDownListFor(model => Model.adjusterLanguages[i].languageID,
(SelectList)ViewBag.ForeignLanguages)
@Html.ValidationMessageFor(model =>Model.adjusterLanguages[i].languageID)
</div>
<div style="clear: left;"></div>
i++;
}
我可以确认正在填充模型数据(来自我的控制器):
model.adjusterLanguages = adjLangs;
那么,当模型被发送到视图时,我怎样才能让它选择项目呢?
生成的html如下:
<div class="editor-field row">
<select id="adjusterLanguages_0__languageID" name="adjusterLanguages[0].languageID"><option value="">--Select--
</option>
<option value="94c5be88-814e-4719-9784-587eb88aa975">Afrikanns</option>
<option value="37b7bf71-7f4e-47c2-8b81-652b0d63dbcb">Albanian</option>
<option value="c94d6d85-0ba2-47d7-932e-2a61feba37b3">Arabic</option>
<!-- ... truncated for brevity ... -->
</select>
<span class="field-validation-valid" data-valmsg-for="adjusterLanguages[0].languageID" data-valmsg-replace="true"></span>
</div>
<div style="clear: left;"></div>
<div class="editor-field row">
<select id="adjusterLanguages_1__languageID" name="adjusterLanguages[1].languageID">
<option value="">--Select--</option>
<option value="94c5be88-814e-4719-9784-587eb88aa975">Afrikanns</option>
<option value="37b7bf71-7f4e-47c2-8b81-652b0d63dbcb">Albanian</option>
<option value="c94d6d85-0ba2-47d7-932e-2a61feba37b3">Arabic</option>
<!-- ... truncated for brevity ... -->
</select>
<span class="field-validation-valid" data-valmsg-for="adjusterLanguages[1].languageID" data-valmsg-replace="true"></span>
</div>