asp-route-id不传递参数值

时间:2016-01-18 08:07:52

标签: asp.net-core asp.net-core-mvc

此表正确填充@device.DeviceId但未将@device.DeviceId传递给控制器​​操作。

<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>

控制器操作签名是

public async Task<IActionResult> DeleteDevice(int deviceId)

调用操作,但deviceId = 0。

如何确保@device.DeviceId正确传递给操作?

1 个答案:

答案 0 :(得分:22)

将控制器操作更改为:

public async Task<IActionResult> DeleteDevice(int id)

或更改a标记,以便asp-route-*属性与参数名称匹配,如下所示:

<a asp-action="DeleteDevice"
   asp-controller="Device"
   asp-route-deviceId="@device.DeviceId">
    Delete
</a>