1、通过对象接收请求数据时的null

必须为对象的属性设置get与set

private System.String _EMail = System.String.Empty;
public System.String EMail
{
get {return _EMail;}
set {_EMail = value;}
}

  

2、通过ajax传递的empty在对象中自动转换为null

第一个解决方案

[DisplayFormat(ConvertEmptyStringToNull = false)]
public string test{get;set;}

第二个解决方案

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace AiAn.GPS.Web.Models
{
//重写DataAnnotationsModelMetadataProvider方法中的CreateMetadata方法
public class MyDataAnnotationsModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
{
var md = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); DataTypeAttribute dataTypeAttribute = attributes.OfType<DataTypeAttribute>().FirstOrDefault();
DisplayFormatAttribute displayFormatAttribute = attributes.OfType<DisplayFormatAttribute>().FirstOrDefault();
if (displayFormatAttribute == null && dataTypeAttribute != null)
{
displayFormatAttribute = dataTypeAttribute.DisplayFormat;
}
if (displayFormatAttribute == null)
{
md.ConvertEmptyStringToNull = false;
} return md;
}
}
}
//在Global.asax的Application_Start方法中,重新覆盖原有对象
ModelMetadataProviders.Current = new AiAn.GPS.Web.Models.MyDataAnnotationsModelMetadataProvider();

  

.net mvc接收参数为null的解决方案的更多相关文章

  1. Spring MVC接收参数(Map,List,JSON,Date,2个Bean)(记录一次面试惨状)

    题目Spring MVC 接收参数 MapListDate2个BeanJSON Spring MVC接收参数 -Map Spring MVC接收参数 -List Spring MVC接收参数 -dat ...

  2. 关于后台接收参数为null的问题之ajax--contentType

    ajax方法中的参数: contentType:发送至服务器时内容的编码类型,一般默认:application/x-www-form-urlencoded(适应大多数的场合) dataType:预期服 ...

  3. MVC 接收参数时会自动解码

    MVC在接收传递过来的参数时,会自动对参数进行解码,无需手动解码 例: public ActionResult SendMsg2(string name) { return Content(name) ...

  4. spring mvc接收参数方式,json格式返回请求数据

    1 使用方法形参使用变量接收提交的数据 2 在方法的形参中使用模型接收数据 3 如果在提交的表单中有多个数据模型,需要创建一个新的Bean,里面的属性是要接收的对象变量. 4 接收提交的日期字符串,转 ...

  5. vue axios 发送post请求,后端接收参数为null

    1首先检查自己的传参方式是否正确,我是传一个对象,没有问题,接口也触发了 2查了下资料说是 Content-Type的问题,设置为   'application/x-www-form-urlencod ...

  6. 关于controller层用实体类接收参数为null的问题

    如果你的表单标签中包含enctype="multipart/form-data"属性,那么请将它删掉<form action="xxxxxxx" id=& ...

  7. 【转】MVC form提交实体接收参数时空字符串值变成null

    问题:entity.BZ的值是null,Request.Form["BZ"]的值是空字符串 目标:让entity.BZ的值是空字符串. 解决方法:在实体的BZ属性上加上 [Disp ...

  8. MVC5的控制器,使用HttpPost方式时,接收的参数为null的原因

    1.问题现象 POST提交时,控制的Action接收到的参数为null, 但Request.Form.Request.Params等集合其实是包含提交的所有数据的 .如下截图: 2.该问题很诡异,重新 ...

  9. 【spring mvc】后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface

    后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate ...

随机推荐

  1. HTML5与HTML4的区别-----新增的常用标签

    做前端工程师这么长时间了, 对HTML5的一些标签的用法还不是很熟悉.这篇随笔算是对学过的知识的梳理.常言道,温故而知新  ~哈哈.里面有不正确的地方还望各位大牛们指正,评论. 在做网页时习惯把网页分 ...

  2. TTStand 基础知识[8] Build-In StepTypes(3)

    Build-In Step Types的最后一篇,前面两篇的连接如下: TestStand 基础知识[7] Build-In StepTypes(2) TestStand 基础知识[6] Build- ...

  3. CVE-2019-0199:Apache Tomcat DDOS

    CVE-2019-0199:Apache Tomcat DDOS 0X00漏洞概述 Apache Tomcat HTTP/2拒绝服务漏洞,该漏洞是由于应用服务允许接收大量的配置流量,并且客户端在没有读 ...

  4. 1215 - Finding LCM

    1215 - Finding LCM   LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LC ...

  5. RJM8L151F6P6温度枪方案对比

    疫情当下,温度计.呼吸机.监护仪.制氧机等医疗产品的生产供应至关重要,红外温度计属于非接触式测温,它是利用物体热辐射与物体温度之间的关系来工作的. 红外测温仪是一种将红外技术与微电子技术相结合的新型温 ...

  6. go程序基于阿里云CodePipeline的一次devops实践

    背景 最近朋友有个项目代码托管用的码云,测试服务器(阿里云ECS)只有一台,三四个人开发,于是想基于阿里云的CodePipeline快速打造一套自动化cicd的流程,使用docker来进行多套环境部署 ...

  7. js , forEach 用法

    person.forEach((item,index) => { console.log( item.id ); if( id == item.id ){ item.is_selected = ...

  8. Android中实现长按照片弹出右键菜单

    场景 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将布局改为Lin ...

  9. [MySQL] mysql索引的长度计算和联合索引

    1.所有的索引字段,如果没有设置not null,则需要加一个字节.2.定长字段,int占4个字节.date占3个字节.char(n)占n个字符.3.变长字段,varchar(n),则有n个字符+两个 ...

  10. winForm学习 2019年4月11日

    1.Directory静态类 相似类:File.Path.StreamReader.StreamWirter 创建文件夹:Directory.CreateDirectory 删除文件夹:Directo ...