解决mvc下dropdownlistfor默认值设置

http://my.oschina.net/kavensu/blog/307376

关键点在于ViewBag.XX不能与字段名同名。否则无法设置默认值。应该是因为冲突。

例如:

@Html.DropDownListFor(model => model.dept_id, ViewBag.depts as IEnumerable<SelectListItem>,string.Empty, new { style = "width:180px" })

不能写成:

@Html.DropDownListFor(model => model.dept_id,ViewBag.dept_id as IEnumerable<SelectListItem>,string.Empty, new { style = "width:180px" })

 

该办法可以解决后端List<SelectListItem>前端无法获取默认值的情况

posted @ 2015-05-26 14:30  贫僧小衲  阅读(707)  评论(0编辑  收藏  举报