我有一个简单的 ajax 调用,例如:
$.ajax({
url: ... ,
data: { anArray: [] },
...
});
和 Controller 的 Action :
public ActionResult Test(int[] anArray) {
...
}
似乎anArray
为 null 而不是空数组。
我也试过
-
List<int>
-
string[]
-
object[]
但对于以上所有内容,我看到 anArray
为空参数。
我读过类似的问题,但与模型参数有关,我这里没有模型。
如何在 Controller 的 Action 中接收一个空数组?我该怎么办?
最佳答案
似乎 MVC 将空数组映射为 null;这是出于某种原因设计的。我想无论如何你不能对零项数组做太多事情。
看来最好的办法是自己实例化数组,如果它确实以 null 形式出现的话:
public ActionResult Test(int[] anArray)
{
anArray = anArray ?? new int[0];
//...
}
关于c# - 从 ajax 发送一个空数组在 ASP.NET MVC 操作中给出了一个空参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33689322/