MVC checkbox报错The value 'on' is not valid for <propertyname>.
来源:互联网 发布:greenplum是什么数据库 编辑:程序博客网 时间:2024/05/17 04:05
在我的MVC项目里,一个Model定义,
public class Admin { public long ID { get; set; } public string Name { get; set; } public bool IsSuperAdmin { get; set; } //......... }编辑该Model的View代码,
<div class=""><input name="IsSuperAdmin" type="checkbox" id="checkIsSuperAdmin" @(Model.IsSuperAdmin ? "checked" : "")> <label class="" for="checkIsSuperAdmin">是否是超级管理员</label></div>后台Controller的代码,
[HttpPost] public ActionResult Update(Admin admin) { if (ModelState.IsValid) { //........... } //............ }这时ModelState.IsValid是false,同时ModelState里存在这样一个错误,Key是IsSuperAdmin,Value是The value 'on' is not valid for IsSuperAdmin。
原因呢?因为checkbox的值value是'on',MVC无法将这个值转换成bool类型。
解决方法,使用@Html.CheckBoxFor/@Html.CheckBox代替元素<input type="checkbox">。当使用@Html.CheckBoxFor/@Html.CheckBox
时,ASP.NET MVC会自动在后面生成一个隐藏的元素,用于保存bool值。这样model绑定时就不会出现这个错误了。修改以后的代码
<div class=""> @Html.CheckBoxFor(model => model.IsSuperAdmin, new { id = "checkIsSuperAdmin" }) <label class="" for="checkIsSuperAdmin">是否是超级管理员</label></div>
1 0
- MVC checkbox报错The value 'on' is not valid for <propertyname>.
- '{DependencyProperty.UnsetValue}' is not a valid value for the '.........Background'
- Android Studio报错:the selected directory is not a valid home for unknow sdk
- PymongoDB报错MongoError: The dotted field .. is not valid for storage
- The prefix "mvc" for element "mvc:annotation-driven" is not bound 报错的解决方法
- 报错:this class is not key value coding-compliant for the key closeLotTextField解决方法
- Hessian调用过程报错Pass-by-value is not supported for the given object
- 报错:this class is not key value coding-compliant for the key tableview
- iOS报错之this class is not key value coding-compliant for the key digitPressed.
- 报错:this class is not key value coding-compliant for the key closeLotTextField解决方法
- eclipse git 报错:the current branch is not configured for pull No value for key branch.xxx.merge found
- eclipse git报错git The current branch is not configured for pull No value for key branch.master.merge
- h:selectOneMenu 报Value is not valid错误
- iOS Xcode 6报错:setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key v
- iOS遇到this class is not key value coding-compliant for the key tableView报错解决办法
- iOS- 报错(崩)[< > setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key
- this class is not key value coding-compliant for the key ... 报这个错误
- The run destination''''is not valid for Running the scheme
- 关于数组的一些操作
- PS
- 微信登录开发-java
- Hadoop HDFS
- Bash 脚本的比较运算符
- MVC checkbox报错The value 'on' is not valid for <propertyname>.
- 有return的情况下try catch finally的执行顺序(最有说服力的总结)
- 虚拟机 ubuntu设置静态IP
- 项目5-- 迷宫问题之图深度优先遍历解法
- 命令行设置代理
- libcurl.lib 导入问题 须知
- java中,一个类实现某个接口,必须重写接口中的所有方法吗???
- 使用nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器(二)
- ubuntu 下tomcat7启动慢