我想根据我的后端数据设置“选定”选项。这是我的代码示例...
<select id="listName" name="listName">
<option>Select Name</option>
@foreach (var item in ViewBag.NameList as Dictionary<string, string>)
{
//worked in MVC2, not with razor
<option value= '@item.Key' @if (TempData["name"].ToString() == item.Key) { selected = ""selected""); }>@item.Value - @item.Key</option>
string nameSelect = TempData["name"].ToString() == item.Key ? "selected" : "";
<option value= '@item.Key' selected= '@nameSelect'>@item.Value - @item.Key</option>
}
</select>
有一个更好的方法吗?