.Net Core RC2 MVC参数在控制器中为空

时间:2016-06-16 14:57:56

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

在VS代码中使用.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();
 }

提交表单时,它会在方法中遇到断点,但password参数为null。我做错了什么?

1 个答案:

答案 0 :(得分:3)

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

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

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

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