所以我有以下(伪代码):
string selectedvalud = "C";
List<SelectListItem> list= new List<SelectListItem>();
foreach(var item in mymodelinstance.Codes){
list.Add(new SelectListItem { Text = item.Name, Value = item.Id.Tostring(), Selected = item.Id.ToString() == selectedvalue ? true : false });
}
ViewBag.ListOfCodes = list;
在我看来:
<%: Html.DropDownList("Codes", (List<SelectListItem>)ViewBag.ListOfCodes , new { style = "max-width: 600px;" })%>
现在,在它到达视图之前,“列表”已经用项目填充它并标记了已经选择的项目。但是当它到达视图时,没有一个选项被标记为选中。
我的问题是,是否可以使用 viewbag 来传递物品或者我应该使用不同的媒介?因为如果我以这种方式使用它,它会删除选项上的选定标志。