Asp.net mvc 3 列表<;int>;url参数行为的参数默认绑定

Asp.net mvc 3 列表<;int>;url参数行为的参数默认绑定,asp.net-mvc-3,Asp.net Mvc 3,我们为什么会有这种行为 控制器操作: public ActionResult TestProc(List<int> list) { return new ContentResult(); } public ActionResult TestProc(列表) { 返回新的ContentResult(); } 请求: /TestProc 导致一个空列表。嗯 /TestProc?列表=[] 导致一个空列表。嗯 /TestProc?列表= 导致一个包含一项0的列表。不行。我假设绑定器

我们为什么会有这种行为

控制器操作:

public ActionResult TestProc(List<int> list)
{
  return new ContentResult();
}
public ActionResult TestProc(列表)
{
返回新的ContentResult();
}
请求:

/TestProc 导致一个空列表。嗯

/TestProc?列表=[] 导致一个空列表。嗯

/TestProc?列表= 导致一个包含一项0的列表。不行。我假设绑定器看到查询字符串存在,它没有值,因此它在列表中推送默认的int值,即它将list=视为与list=0相同的值。我觉得这令人困惑。我本以为list=在这个上下文中是相同的,根本没有列表,我希望list为null

我觉得这令人困惑

我同意。在这种情况下,可以使用可为空的整数:

public ActionResult TestProc(List<int?> list)
public ActionResult TestProc(列表)

这一次,模型绑定器将推送一个
null
的元素,您将能够在
/TestProc?list=
/TestProc?list=0
之间消除歧义,如果您必须这样做的话。

是的,您甚至可以争辩说,任何时候(除非有异常),模型绑定器都会看到不可为null的类型的空值,它应该将其视为具有无效字符。但是我想如果你考虑整数的行为,它可以是预期的行为。因为?list=0&list=1将绑定到list={0,1}