Home > Back-end >  Parse Id from view to controller
Parse Id from view to controller

Time:10-13

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  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);
}

CodePudding user response:

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

CodePudding user response:

1.Add input a name attribute 2.remove route id

    <input  type="submit" value="Terminal" asp-controller="Terminal" asp-action="TerminalInfo" name="storeLicenseId">

OR: Use asp-for

  • Related