为什么ASP.Net MVC模型联编程序将空的JSON数组绑定为null? [英] Why does the ASP.Net MVC model binder bind an empty JSON array to null?

查看:100
本文介绍了为什么ASP.Net MVC模型联编程序将空的JSON数组绑定为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的模特班:

public class MyModel
{
    public Employees[] MyEmpls{get;set;}
    public int Id{get;set;}
    public OrgName{get;set;}
}

将带有MyEmpls as empty array的以下JSON结构对象传递给MVC控制器.

Passing the below JSON structure object with MyEmpls as empty array to MVC controller.

["Id":12, "MyEmpls":[], "OrgName":"Kekran Mcran"]

控制器

[HttpPost]
public ActionResult SaveOrg(MyModel model)
{
  //model.MyEmpls is null here
}

我期望mode.MyEmpls是一个空的c#数组,而不是null.自定义模型绑定程序是否需要一个空数组?

I am expecting mode.MyEmpls to be an empty c# array, not a null. Is a custom model binder necessary to achieve an empty array?

推荐答案

我认为其他一些答案都没有回答问题的含义:为什么默认的MVC模型联编程序将空的Json数组绑定为null而不是空的C#数组?

I think that some of the other answers have missed the meaning of the question: why does the default MVC model binder bind an empty Json array to null instead of an empty C# array?

好吧,我不能告诉你他们为什么这样做,但是我可以告诉你它发生的地方.可在以下位置的CodePlex上找到MVC的源: http://aspnetwebstack.codeplex.com/SourceControl/latest .您要查找的文件是 ValueProviderResult.cs ,您可以在其中看到:

Well, I can't tell you why they did that, but I can show you where it happens. The source for MVC can be found on CodePlex here: http://aspnetwebstack.codeplex.com/SourceControl/latest. The file you're looking for is ValueProviderResult.cs where you can see:

    private static object UnwrapPossibleArrayType(CultureInfo culture, object value, Type destinationType)
    {
        if (value == null || destinationType.IsInstanceOfType(value))
        {
            return value;
        }

        // array conversion results in four cases, as below
        Array valueAsArray = value as Array;
        if (destinationType.IsArray)
        {
            Type destinationElementType = destinationType.GetElementType();
            if (valueAsArray != null)
            {
                // case 1: both destination + source type are arrays, so convert each element
                IList converted = Array.CreateInstance(destinationElementType, valueAsArray.Length);
                for (int i = 0; i < valueAsArray.Length; i++)
                {
                    converted[i] = ConvertSimpleType(culture, valueAsArray.GetValue(i), destinationElementType);
                }
                return converted;
            }
            else
            {
                // case 2: destination type is array but source is single element, so wrap element in array + convert
                object element = ConvertSimpleType(culture, value, destinationElementType);
                IList converted = Array.CreateInstance(destinationElementType, 1);
                converted[0] = element;
                return converted;
            }
        }
        else if (valueAsArray != null)
        {
            // case 3: destination type is single element but source is array, so extract first element + convert
            if (valueAsArray.Length > 0)
            {
                value = valueAsArray.GetValue(0);
                return ConvertSimpleType(culture, value, destinationType);
            }
            else
            {
                // case 3(a): source is empty array, so can't perform conversion
                return null;
            }
        }
        // case 4: both destination + source type are single elements, so convert
        return ConvertSimpleType(culture, value, destinationType);
    }
}

有趣的部分是案例3":

The interesting part is "case 3":

else
{
    // case 3(a): source is empty array, so can't perform conversion
    return null;
}

您可以通过在其构造函数中的模型上初始化数组来避开此问题.在快速阅读源代码时,我无法告诉您为什么他们不能返回空数组或为什么他们决定不返回空数组,但这应该引起有趣的阅读.

You can sidestep this issue by initialising your array on the model in its constructor. In my quick reading of the source I can't tell you why they can't return an empty array or why they decide not to, but it should make for interesting reading.

这篇关于为什么ASP.Net MVC模型联编程序将空的JSON数组绑定为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆