zoukankan      html  css  js  c++  java
  • .net mvc接收参数为null的解决方案

    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();
    

      

  • 相关阅读:
    pdf 去水印 比较好用
    ffmpeg从视频中每隔几秒提取一张图片
    tampermonkey 网盘直链下载助手
    淘工厂买的usb 蓝牙5.0 外置台式机 适配器(下载)
    Apache Doris 轻松入门和快速实践
    一步一步编译最新版Apache Doris 0.15版本的详细过程
    go使用msgpack
    服务接口杂谈
    关于真伪3层所讨论的
    jattach最近的一些更新
  • 原文地址:https://www.cnblogs.com/zhoushangwu/p/9641491.html
Copyright © 2011-2022 走看看