您现在的位置是:网站首页> 编程资料编程资料
解决asp.net mvc UpdateModel更新对象后出现null问题的方法_实用技巧_
2023-05-24
286人已围观
简介 解决asp.net mvc UpdateModel更新对象后出现null问题的方法_实用技巧_
在用asp.net mvc 4.0做项目的时候遇到的这种情况:
情况分析:
“在填写表单的时候,有一些表单没有填写,留空,然后直接post 提交表单,action中用UpdateModel 来更新model,结果发现那些没有填写的表单字段全部变成null。”
原因分析:
项目中做了判断null不能提交更新到数据库中,所以导致一直提交不上去
后来网上查了一下找到了解决办法,我在这里分享一下,方便以后遇到这种情况的朋友可以方便解决
解决方法:
新建一个类继承DefaultModelBinder
using System.ComponentModel; using System.Web.Mvc; namespace CustomerWebsite.Mvc { public sealed class EmptyStringToNullModelBinder : DefaultModelBinder { protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value) { if (value == null && propertyDescriptor.PropertyType == typeof(string)) { value = string.Empty; } base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value); } } } 然后在Global.asax的Application_Start中替换DefaultModelBinder
ModelBinders.Binders.DefaultBinder = new EmptyStringToNullModelBinder();
这样问题就可以解决了,小编也尝试进行了操作,结果成功了,希望也能帮助这方面有困扰的童鞋解决实际问题。
您可能感兴趣的文章:
- asp.net“服务器应用程序不可用” 解决方法
- ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法
- ASP.NET MVC使用Ajax的辅助的解决方法
- ASP.NET MVC中解析淘宝网页出现乱码问题的解决方法
- asp.net中mvc使用ajax提交参数的匹配问题解决探讨
- 使用asp.net MVC4中的Bundle遇到的问题及解决办法分享
- ASP.NET在MVC中MaxLength特性设置无效的解决方法
- asp.net mvc3.0安装失败如何解决
- ASP.NET MVC命名空间时引起错误的解决方法
- 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式问题的方法
相关内容
- 总结十条.NET异常处理建议_实用技巧_
- 浅析.NET逻辑分层架构_实用技巧_
- Microsoft Visual Studio 2010下如何添加命令提示行_实用技巧_
- 分享Visual Studio原生开发的10个调试技巧(2)_实用技巧_
- Visual Studio调试技巧汇总_实用技巧_
- ASP.NET如何使用web服务的会话状态_实用技巧_
- asp.net中“从客户端中检测到有潜在危险的Request.Form值”错误的解决办法_实用技巧_
- ASP.NET中Web API的简单实例_实用技巧_
- 创建一个完整的ASP.NET Web API项目_实用技巧_
- ASP.NET MVC中图表控件的使用方法_实用技巧_
