C# MVC开发DropListFor默认值选中

来源:互联网 发布:xml转json 编辑:程序博客网 时间:2024/05/17 17:43

这个问题主要是写代码习惯造成。

解决方案就是

将ViewData的名字与字段名字不能使用同一个。就能使用model默认显示了。

例子:

controller中的内容

ViewData["Category"] = items;

View中内容

    @Html.DropDownListFor(model => model.ArticleCategory, ViewData["Category"] as IEnumerable<SelectListItem>, new { @class = "col-xs-10 col-sm-5" })                           

注意

ArticleCategory

 ViewData["Category"]
这两个名字绝对不能一样,否则就会导致默认值不能显示。


0 0