页面是这样写的
<%= Html.DropDownList("pSubtractionOperator", (IList<SelectListItem>)ViewData["pSubtractionOperator"], new { })%>
编辑的时候在控制器中调用此方法,传入了选中的值,往页面传递ViewData["pSubtractionOperator"],但是到了页面匹配的项不会被选中,高手帮忙解答(dubug时没有发现问题)
private IList<SelectListItem> GetOperatorListItem(string selectValue)
{
//获取所有的项
IList<SubtractionOperator> list = _operatorService.GetAll();
IList<SelectListItem> selectList = new List<SelectListItem>();
foreach (var item in list)
{
selectList.Add(new SelectListItem() { Text = item.Name, Value = item.SubtractionOperatorID.ToString(), Selected = selectValue == item.SubtractionOperatorID.ToString() });
}
return selectList;//返回IList<SelectListItem>
}
<%=Html.DropDownList("PostType", new SelectList((List<PostType>)ViewData["PostType"], "value", "key", 0), new {style="width:200px;" })%>
红色为选中的ID值