skip to Main Content

I’m having a strange issue, I have a populated datatable and I’m trying to pass the StoreLicenseId to a method in my controller but the parameter in my method is always null. The StoreLicenseId is in my datatable. Everything looks correct but I just can’t get it to work.

View

<form method="post">
    <input class="btn btn-outline-info btn-1" type="submit" value="Terminal" asp-controller="Terminal" asp-action="TerminalInfo" asp-route-id="@item.StoreLicenseId">
</form>

Controller

public IActionResult TerminalInfo(string storeLicenseId)
{
    if (_context.StoreLicenses == null)
    {
        return NotFound();
    }

    var terminalModel = _context.StoreLicenses.FindAsync(storeLicenseId);
    if (terminalModel == null)
    {
        return NotFound();
    }
    return View(terminalModel);
}

2

Answers


  1. You should set your asp-route parameter to the same name of the parameter expected by the controller action, so change this

     asp-route-id="@item.StoreLicenseId"
    

    to

    asp-route-storeLicenseId="@item.StoreLicenseId"
    

    or change the name in the controller action to be simply id

    Login or Signup to reply.
  2. 1.Add input a name attribute
    2.remove route id

        <input class="btn btn-outline-info btn-1" type="submit" value="Terminal" asp-controller="Terminal" asp-action="TerminalInfo" name="storeLicenseId">
    

    OR:
    Use asp-for

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search