asp-route-id无法访问ViewModel属性

时间:2016-01-18 02:42:10

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

在此表中

<table border="1" cellpadding="10">
@foreach (var device in @Model.Devices)
{
    <tr>
        <td>@device.DeviceID</td>
        <td>@device.DeviceName</td>
        <td>@device.CALs</td>
        <td align="right">@device.UnlockedFrom.ToShortDateString()</td>
        <td align="right">@device.UnlockedTo.ToShortDateString()</td>
        <td>
            <a asp-action="DeleteDevice"
               asp-controller="Manage"
               asp-route-id="@device.DeviceId">
                Delete
            </a>
        </td>
  </tr>
}

<td>@device.DeviceID</td>很好但asp-route-id="@device.DeviceId给出了一个波浪形的device does not contain a definition for DeviceId

viewmodel公开了一组设备

public IEnumerable<Device> Devices { get; set; }

Device包含以下属性

    public int DeviceID { get; set; }
    public string UserId { get; set; }
    public virtual ApplicationUser User { get; set; }
    public int CALs { get; set; }
    public string DeviceName { get; set; }
    public DateTime UnlockedFrom { get; set; }
    public DateTime UnlockedTo { get; set; }

如何使DeviceId可以传递给控制器​​?

1 个答案:

答案 0 :(得分:1)

因为您的媒体资源名称为DeviceID,而非DeviceId。作为asp-route-*属性的值传递的属性名称区分大小写。

将您的代码更改为

<a asp-action="DeleteDevice"  asp-controller="Home"
                                              asp-route-id="@device.DeviceID">Delete </a>