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. ORACLE-SQLLOAD导入外部数据详解

    今天公司需要把外部文本的一些数据导入到数据库.这里把相关步骤和注意的地方记录,供需要的人参考学习!这里的环境是在windows下的数据库,linux或者其他数据库同理! 1.准备工作:创建需要导入数据 ...

  2. Linux系统基础认知

    什么是操作系统? 操作系统作为接口的示意图: 没有安装操作系统的计算机,通常被称为裸机 如果想在 裸机 上运行自己所编写的程序,就必须用机器语言书写程序 如果计算机上安装了操作系统,就可以在操作系统上 ...

  3. O准备如何苟进复赛圈?华为软挑开挂指南(附赛题预测)

    事先声明,这不是华为软挑的软广,我也不是海军. 这篇文章纯粹是心血来潮,原因是去年上传到github的参赛代码,前几天又有两个人star和fork了. 记得star热潮还是去年4月复赛刚结束的那几天, ...

  4. False注入,以及SQL注入技巧总结

    title: False注入,以及SQL注入技巧总结 date: 2017-04-25 00:23:31 tags: ['SQL注入'] --- 利用False我们可以绕过一些特定的WAF以及一些未来 ...

  5. [Effective Java 读书笔记] 第二章 创建和销毁对象 第六-七条

    第六条 消除过期引用 JAVA中依然会有 memory leak的,比如一个栈先增长再收缩,那么从栈中弹出的对象是不会被当做垃圾回收的,即时使用栈的程序不再引用这些对象.这是因为栈的内部维护着对这些对 ...

  6. RFC笔记—IP Version 6 Addressing Architecture

    IP Version 6 Addressing Architecture,RFC4291 It includes the basic formats for the various types of ...

  7. k8s系列---kubectl基础

    kubectl get pods  查看所有pods kubectl  get  services  查看services kubectl replace --filename=myweb-rc.ya ...

  8. 意法半导体STM32MP157A MPU加持,米尔科技首款ST Linux开发板MYD-YA157C评测

    ST公司去年推出了MPU系列芯片,MPU系列不同于以往产品,它既包含有ARM公司Cortex M 单片机核心,也包含有ARM公司Cortex A 应用处理器核心,以期将STM32单片机产品优势扩展到更 ...

  9. IP切换脚本

    1. 新建bat文件: 2. 将下面内容拷贝进bat文件: 3. 运行bat文件: @echo off color 00title IP切换脚本:start @echo --------------- ...

  10. vue路由--命名视图

    有时候想同时(同级)展示多个视图,而不是嵌套展示,例如创建一个布局,有 sidebar(侧导航) 和 main(主内容) 两个视图,这个时候命名视图就派上用场了.你可以在界面中拥有多个单独命名的视图, ...