是否有一种简单的方法可以从另一个模型中的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>
我正在尝试将@ Rrl.Action中的其他参数与角色ID一起传递,但是我似乎无法弄清楚将其引入的秘密,因此可以将其传递给后端控制器。
答案 0 :(得分:3)
问题是
public IList<string> Roles { get; set; }
仅包含字符串,因此没有要查找的ID。您必须将此行更改为
public IList<ManageUserRoleViewModel> Roles { get; set; }
这样,您就有了一个列表,其中也包含一个ID。然后,您可以执行以下操作:
@Model.Roles.FirstOrDefault(x => x.RoleId == YOUR_UNIQUE_ID)
这将为您提供一个对象,以执行进一步的逻辑。