SheUsesVaseline
SheUsesVaseline

Reputation: 1

Parse Id from view to controller

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

Upvotes: -1

Views: 142

Answers (2)

Murad
Murad

Reputation: 62

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

Upvotes: 1

Steve
Steve

Reputation: 216291

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

Upvotes: 2

Related Questions