首页 > 技术文章 > MVC Dropdownlist数据绑定 默认值

xbzhu 2018-04-13 16:54 原文

@Html.DropDownList("Data", (SelectList)ViewBag.Data, new { @class = "form-control chosen-select-disable_search", data_placeholder = "请选择..." })

 

List Data= new List();
Data.Add(new SelectListItem { Text = "AA", Value = "1" });
Data.Add(new SelectListItem { Text = "BB", Value = "2", Selected = true });
ViewBag.Data= Data;


按照上面的代码给Dropdownlist数据源设置了默认值,但实际上预览的时候并没有绑定默认值。后来研究发现,ViewBag的参数名和Dropdownlist的name不能重名

将cshtml中的代码改为下面这样既可。

 

@Html.DropDownList("Data1", (SelectList)ViewBag.Data, new { @class = "form-control chosen-select-disable_search", data_placeholder = "请选择..." })

 

推荐阅读