@Html.DropDownList,绑定默认值

 

 

后端代码:

List<Area> list = new List<Area> {
new Area { Id=1,Name="北京"},
new Area { Id=2,Name="上海"},
new Area { Id=3,Name="深圳"}
};

SelectList areas = new SelectList(list, "Id", "Name", 2);
ViewData["area"] = areas;

前端代码:

@Html.DropDownList("area", ViewData["area"]);

执行后,默认值显示不出来的。

改成:

@Html.DropDownList("sel_area", ViewData["area"]);

执行后,默认值显示成功。

改成:

@Html.DropDownList("area", ViewData["list_area"]);

执行后,默认值显示成功。

原因分析,DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList),name和ViewData["area"],

如果name 和ViewData的key值命名成一样,默认值显示不出来,目前为止不知道什么原因,先记着,以后再看。如果要显示默认值,

保证name 和ViewData的key值不一样就行了。

 

posted on 2019-12-01 16:44  鱼重生  阅读(809)  评论(0)    收藏  举报