Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp-route-id does not pass parameter value

This table correctly populates with @device.DeviceId but does not pass @device.DeviceId to the controller action.

<table border="1" cellpadding="10">
    @foreach (var device in @Model.Devices)
    {
        <tr>
            <td>@device.DeviceId</td>
            <td>@device.DeviceName</td>
            <td>
                <a asp-action="DeleteDevice"
                   asp-controller="Device"
                   asp-route-id="@device.DeviceId" asp->
                    Delete
                </a>
            </td>
        </tr>
    }
</table>

The controller action signature is

public async Task<IActionResult> DeleteDevice(int deviceId)

The action is called but deviceId = 0.

How do I ensure @device.DeviceId is correctly passed to the action?

like image 894
Vague Avatar asked Jan 18 '16 08:01

Vague


1 Answers

Either change the controller action to:

public async Task<IActionResult> DeleteDevice(int id)

or, change the a tag so that the asp-route-* attribute matches the name of the parameter, like so:

<a asp-action="DeleteDevice"
   asp-controller="Device"
   asp-route-deviceId="@device.DeviceId">
    Delete
</a>
like image 147
Jamie Dunstan Avatar answered Oct 03 '22 02:10

Jamie Dunstan