Search code examples
c#razorasp.net-coretag-helpers

Can't redirect the submit to an action


I have the following actions and view following the docs.

[HttpPost("validate")]
public IActionResult Validate([FromQuery] string user, string pass)
{ ... }

[HttpGet("login")]
public IActionResult LogIn([FromQuery] string returnUrl)
{ 
  ...
  return View(model); 
}
@using Microsoft.AspNetCore.Mvc.TagHelpers;
@model Dictionary<string, string>

<form asp-controller="account" asp-action="validate" method="post">
  <input asp-for="@Model["user"]" placeholder="Username" />
  <input asp-for="@Model["pass"]" placeholder="Password" />
  <button type="submit">Go!</button>
</form>

Upon click on the button, I get back to the same action (i.e. LogIn) insteda of the specified (i.e. Validate). It seems like the only thing I control in the form is the method. I've added using statement for accessing the library of TagHelpers but it seems it doesn't do jack difference.

What can I have missed?


Solution

  • The final rendition (ctrl+u in Chrome or F12 and check for the elements) produces <form asp-controller="account" asp-action="validate" method="post">...<button type="submit">Check the credentials</button> </form>. I was expecting the asp-xxx attributes to be transformed. Was I mistaken?

    No, you are correct. These tag helper attributes should have been transformed into a single action attribute that contains the target URL to your Validate action.

    Since the attributes were not transformed that means that the tag helpers did not run. This is most likely caused by a missing @addTagHelper directive.

    You need to have the following line either in your view itself, or in the _ViewImports.cshtml file (where the line should be by default):

    @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    

    Importing the namespace with a @using directive is not necessary.