.net mvc Html.DropDownListFor 设置默认值无效
来源:互联网 发布:手机间谍软件官方网站 编辑:程序博客网 时间:2024/05/18 00:28
在项目中经常用到Html.DropDownListFor,但是很多时候使用的时候只能显示候选列表,在后台设置的默认值无法在前台正确显示;今天又遇到这个问题了,百度无果,在StackOverFlow上找到了答案,总结一下,希望帮到也被这个问题困扰的人。
先抛上没错误的写法:
下拉框的数据源来源于enum中的项:
public enum VideoState { [Display(Name ="等待收益")] EmptyAvaiable =1, [Display(Name ="可提现")] WithDrawAvaiable =2, [Display(Name ="申请提现")] ApplyWithDraw=3, ...}
视图模型中定义了该字段State以及数据源:
public class VideoEdit{ ... public int State { get; set; } public IEnumerable<SelectListItem> StateList { get; set; }}
【注意】上面State属性的类型必须为int,不能是定义的 枚举类型VideoState
在视图中如下使用:
@Html.DropDownListFor(m=>m.State,Model.StateList)
控制器如下传递数据:
public ActionResult Edit(int? id){ ... Video v = db.Videos.Find(id); ... VideoEdit m = new VideoEdit(); //这里是将枚举类转为IEnumerable<SelectListItem> //类型的自定函数,参数是默认值 m.StateList = EnumExtention.ToSelectList<VideoState> ((int)v.State); //这里是视图模型从实体模型继承数据的自定义函数 return View(m.UpdateWithObject(v)); }
下面再分析一下容易错的地方
1.很多才用了枚举的朋友应该是直接用枚举类型作为属性的类型,比如:
public enum VideoState{ A=1,B=2}public class ViewModel{ ... public VideoState State{get;set;}}
这里State属性的类型不能试VideoState了,必须是int,和SelectListItem中Value的类型保持一致,否则会出现设置了默认值,但没有选中的情况
2.用ViewBag传递数据源的名字和视图模型中的名字相同,有冲突。比如:
public class employee_insignia{ public int id{get;set;} public string name{get;set;} //This property will store insignia id public int insignia{get;set;}} // If your ViewBag's name same as your property name ViewBag.Insignia = new SelectList (db.MtInsignia.AsEnumerable(),"id", "description", 1);
视图:
@Html.DropDownListFor(model => model.insignia, (SelectList)ViewBag.Insignia, "Please select value")
这样也会导致默认值未被选中,应该采用如下写法:
ViewBag.InsigniaList = new SelectList(db.MtInsignia.AsEnumerable(), "id", "description", 1);
视图:
@Html.DropDownListFor(model => model.insignia, (SelectList)ViewBag.InsigniaList , "Please select value")
3.属性State的值为null时,即使设置在IEnumerable 中设置了Selected=true也会出现未选中的情况
必须给State赋一个有效的值
以上是几种主要的原因,如有其它问题欢迎补充,转载请注明出处
阅读全文
0 0
- .net mvc Html.DropDownListFor 设置默认值无效
- @Html.DropDownListFor 设置默认值
- 关于.net mvc中@Html.DropDownListFor和@Html.DropDownList默认值无法选中问题
- ASP.NET MVC中@Html.DropDownListFor使用方法
- ASP.NET MVC中@Html.DropDownListFor使用方法
- ASP.NET MVC中@Html.DropDownListFor使用方法
- ASP.NET MVC中@Html.DropDownListFor使用方法
- Asp.net MVC4 Html.DropDownListFor默认值问题的奇葩解决
- ASP.NET MVC 2.0 Html.DropDownListFor默认项设置的小技巧
- 关于mvc中@Html.DropDownListFor和@Html.DropDownList默认值无法选中问题简单总结
- ASP.NET MVC DropDownList扩展,实现foreach@Html.DropDownListFor(p=>p.Type,p.Type,TypeItem)
- .net MVC DropDownListFor控件的使用
- oracle 设置默认值无效
- ASP.NET.MVC--注解和反射将Enum转换List<SelectListItem>为@Html.DropDownListFor()下拉列表的通用方法
- ASP.NET MVC 让@Html.DropDownList显示默认值
- ASP.NET MVC 让@Html.DropDownList显示默认值
- MVC Html.DropDownList 和DropDownListFor 的常用方法
- MVC Html.DropDownList 和DropDownListFor 的常用方法
- 迁移图片目录的bat代码
- 山东省第四届ACM程序设计大赛 Problem A:Rescue The Princess (计算几何)
- Linux操作系统基础——vi篇
- post类型接口测试
- Python归并排序算法
- .net mvc Html.DropDownListFor 设置默认值无效
- java中多线程编程
- UVa 201 Squares
- 【简记】大规模Web开发技术(第十二章)
- uestc L
- python3学习--4 字典
- 剑指offer 面试题29 数组中出现次数超过一半的数字
- linux tomcat具体安装
- 我是一个线程