Search code examples
c#asp.net-mvc.net-core-rc2

.Net Core RC2 MVC parameter is null in controller


Using .NET Core RC2 in VS Code I have the following 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>

And a controller

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

When submitting the form, it hits my breakpoint in the method but the password parameter is null. What do I do wrong?


Solution

  • The form field name should match with the parameter name. So add a name attribute.

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

    You may also remove the [FromBody] decoration.

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