【发布时间】:2019-10-07 13:19:38
【问题描述】:
有没有一种简单的方法可以从另一个模型中的 IList 获取 id?最好用剃须刀?我想在 IList 角色中获取 RoleId。
public class EditUserViewModel
{
public EditUserViewModel()
{
Claims = new List<string>(); Roles = new List<string>();
}
public string Id { get; set; }
[Required]
public string UserName { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
public string City { get; set; }
public List<string> Claims { get; set; }
public IList<string> Roles { get; set; }
}
}
public class ManageUserRoleViewModel
{
public string RoleId { get; set; }
public string RoleName { get; set; }
public bool IsSelected { get; set; }
//Viewbag is used to store UserId
}
public class UserRoleViewModel
{
public string UserId { get; set; }
public string UserName { get; set; }
public bool IsSelected { get; set; }
//Viewbag is used to store UserId
}
<table class="table table-hover table-md ">
<thead>
<tr>
<td class="text-left TableHead">Role</td>
<td class="text-right TableHead">Delete</td>
</tr>
</thead>
@*--Table Body For Each to pull DB records--*@
<tbody>
@foreach (var role in Model.Roles)
{
<tr>
<td>@role</td>
<td>
<button class="sqButton btnRed float-right zIndex" id="Delete" title="Delete" data-toggle="ajax-modal" data-target="#deleteRoleUser" data-url="@Url.Action("Delete", "Administration", new {Id = Model.Id , Type = "roleUser"})">
<i class="glyphicon glyphicon-remove"></i>
</button>
</td>
</tr>
}
</tbody>
</table>
我正在尝试将角色 ID 与 @Url.Action 中的其他参数一起传递,但我似乎无法弄清楚将其拉入的秘密,因此我可以将其传递给后端控制器。
【问题讨论】:
-
但是您的
Model.Roles列表项没有 RoleId,因为它是List<string>... -
在
EditUserViewModel中,您必须使用完整的Role对象,而不仅仅是string,如下所示:public class EditUserViewModel { /* ... */ public IList<Role> Roles { get; set; } }
标签: c# asp.net-mvc razor