此表正确填充@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
正确传递给操作?
答案 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>