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. CodeIgniter框架使用总结

    CodeIgniter框架 1.回忆MVC 1.1.M:模型,提供数据,保存数据 1.2.V:视图,只负责显示,表单form 1.3.C:控制器,协调模型和视图 1.4.action:动作,是控制器中 ...

  2. 基于 Serverless Component 全栈解决方案

    什么是 Serverless Component Serverless Component 是 Serverless Framework 的,支持多个云资源编排和组织的场景化解决方案. Serverl ...

  3. error C2662

    原因:关于const的问题 具体错误:函数的参数列表中参数签名为const,但是却调用了该参数的非const的成员函数 例子: 即使我们知道NoConst()并不会改变类的data成员,编译器依旧会报 ...

  4. 腾讯云Centos服务器部署问题

    在Centos7上部署Tomcat过程 下载并安装JDK 下载并部署Tomcat 打开Centos中对应的端口(默认80) 这几步操作很清楚,但首次使用Centos7时遇到了一个问题,外网无法访问服务 ...

  5. 一次修改数据库物理文件造成Mysql宕机的恢复记录

    事件起始 某夜,我正在床上冥想准备入睡,忽然同事向我求救:消息内容如下: Oh My Gold 改了些配置,啥都没了!都没了!没了!了! 我仔细询问,原来是她因为某些原因将某库的物理文件夹改名后,发现 ...

  6. 转:RBAC如何设计一个权限系统

    前言 权限管理是所有后台系统的都会涉及的一个重要组成部分,主要目的是对不同的人访问资源进行权限的控制,避免因权限控制缺失或操作不当引发的风险问题,如操作错误,隐私数据泄露等问题.目前在公司负责权限这块 ...

  7. raid知识梳理及其详细介绍

    1 raid级别 生产环境常用到的raid级别有raid0,raid1,raid5,raid10.所以侧重学习这几种raid级别即可. 1.1 raid0条带卷 raid0示意图: raid0特点介绍 ...

  8. sed知识及常用用法梳理

    1.sed命令简介及其参数说明 sed流编辑器,擅长对文本进行增删改查,过滤指定的字符串和取指定行,也可以在行中字符串前后插入内容,功能非常强大. 注意:sed默认只支持基本的正则表达式,如果要想支持 ...

  9. HDU 2018 DP

    A - 母牛的故事 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  10. git报错:fatal: No configured push destination.

    本地仓库代码(git push)上传git仓库报错: fatal: No configured push destination. Either specify the URL from the co ...