【问题标题】:.Net Core RC2 MVC parameter is null in controller.Net Core RC2 MVC 参数在控制器中为空
【发布时间】:2016-06-17 00:17:27
【问题描述】:

在 VS Code 中使用 .NET Core RC2 我有以下 HTML

<form asp-controller="Home" asp-action="Connexion" method="post">
    <div class="col-md-4 input-group">
        <input type="text" id="password" class="form-control" placeholder="Mot de passe">
        <span class="input-group-btn">
            <button class="btn btn-secondary" type="submit">Envoyer</button>
        </span>
    </div>
</form>

还有一个控制器

 [HttpPost("/Connexion")]
 public IActionResult Connexion([FromBody] string password)
 {
      return View();
 }

提交表单时,它在方法中命中了我的断点,但密码参数为空。我做错了什么?

【问题讨论】:

  • 浏览器使用输入控件的name属性向服务器提交数据。 id 属性是客户端的东西。

标签: c# asp.net-mvc .net-core-rc2


【解决方案1】:

表单字段名称应与参数名称匹配。所以添加一个名称属性。

<input type="text" name="password" class="form-control" placeholder="Mot de passe">

您也可以移除 [FromBody] 装饰。

[HttpPost("/Connexion")]
public IActionResult Connexion(string password)
{
    return View();
}

【讨论】:

  • 这样做并删除控制器中的 [FromBody] 有效!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-05
  • 2021-05-17
  • 2014-02-21
  • 1970-01-01
  • 2020-10-12
  • 2016-10-13
  • 2016-10-15
相关资源
最近更新 更多