ASP.NET MVC Int Array parameter with empty array defaults to {0}

后端 未结 2 1643
野趣味
野趣味 2021-01-18 19:42

I have a controller action like:

Public ActionResult MyAction(int[] stuff){}

I make a JSON request like:

$.getJSON(url, { s         


        
2条回答
  •  醉酒成梦
    2021-01-18 20:10

    I think this is a bug in MVC:

    // create a vpr with raw value and attempted value of empty string
    ValueProviderResult r = new ValueProviderResult("", "", System.Globalization.CultureInfo.CurrentCulture);
    // this next line returns {0}
    r.ConvertTo(typeof(int[]));
    

    If we look at ValueProviderResult.cs in function UnwrapPossibleArrayType, we see:

    // 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;
    

    It forces converted[0] to be element, and ConvertSimpleType casts "" to 0. So I'm closing this question, unless someone has more info.

    EDIT: Also, this is not in revision 17270, so if you're making a list of things which change from MVC 1 to MVC 2, this is one of them.

提交回复
热议问题