[HttpGet]
public ActionResult Edit(int id)
{
var userToEdit = _db.AuthLogin.Where(user => user.Id == id).SingleOrDefault();
if (userToEdit == null)
return HttpNotFound();
return View(new EditUser()
{
Email = userToEdit.Email,
UserName = userToEdit.UserName,
Password = userToEdit.Password,
Roles = _db.Roles.Select(role => new RoleCheckBox
{
Id = role.Id,
IsChecked = userToEdit.Roles.Contains(role),
Name = role.Name
}).ToList()
});
}
答案 0 :(得分:0)
我不知道你的Role
课是如何定义的,但你可以尝试这样的事情:
List<int> userRoleIds = userToEdit.Roles.Select(r => r.Id).ToList();
EditUser model = new EditUser()
{
Email = userToEdit.Email,
UserName = userToEdit.UserName,
Password = userToEdit.Password,
Roles = _db.Roles.Select(role => new RoleCheckBox
{
Id = role.Id,
IsChecked = userRoleIds.Contains(role.Id),
Name = role.Name
}).ToList()
});
return View(model);
或更改
_db.Roles.Select(...)
到
_db.Roles.ToList().Select(...)