【问题标题】:Parameter passed to controller is null传递给控制器​​的参数为空
【发布时间】:2014-01-20 18:30:01
【问题描述】:

当我尝试发布表单时,控制器中的方法参数为空。那么代码有什么问题呢?

index.cshtml

@using (Html.BeginForm("Index", "Settings",  FormMethod.Post, new { id = "test1" }))
{
  <input type="submit" name="SaveButton" value="Save" />
}

设置控制器-

[HttpPost]
public ActionResult Index(string id)
{
   return View();
}

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 view parameters controller


    【解决方案1】:

    使用FormExtensions.BeginForm Method (HtmlHelper, String, String, Object, FormMethod, Object)

    @using (Html.BeginForm("Index", "Settings",  new { id = "test1" }, FormMethod.Post, null))
    {
      <input type="submit" name="SaveButton" value="Save" />
    }
    

    您正在使用FormExtensions.BeginForm Method (HtmlHelper, String, String, FormMethod, Object),其中object 是为元素设置的HTML 属性。目前是设置form标签的id。

    【讨论】:

      【解决方案2】:

      查看页面的来源...

      new { id = "test1" } 设置表单标签的 id。您需要在表单的花括号内设置某种输入标记,就像您对提交输入所做的那样。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-14
        • 1970-01-01
        • 2015-01-25
        • 2015-09-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多