为什么我在参数操作方法上获得空条目?

时间:2016-08-26 14:06:04

标签: c# asp.net-mvc

我有一个控制器有这样的动作:

    [HttpPost]
    public ActionResult Assign(int reportId, List<int> clientIds)
    {

          // stuff
          return View();
    }

当我从这样的表格中调用它时:

    @using (Html.BeginForm("Assign", "Report", FormMethod.Post, new { reportId = 1} ))
    {
      ...
          <input type="checkbox" name="clientIds" value="@item.Id" checked>

我收到错误

  

参数字典包含参数&quot; reportId&#39;的空条目。非可空类型的System.Int32&#39; for method&#39; System.Web.Mvc.ActionResult Assign(Int32,System.Collections.Generic.List`1 [System.Int32])&#39;

据我所知,控制器应该为reportId获取值1 - 任何人都可以看到我出错的地方吗?

我意识到我可以(应该)使用视图模型从客户端获取数据到动作,但我现在需要这样做...

1 个答案:

答案 0 :(得分:1)

您使用BeginForm()的错误重载并添加reportId作为html属性,而不是路由值。您需要使用this overload,其中第3个参数为object routeValues

@using (Html.BeginForm("Assign", "Report", new { reportId = 1 }, FormMethod.Post))