我有一个我在视图中使用的模型:
using (var db = new FieldSaverContext())
{
AspNetUser user = (from p in db.AspNetUsers
where p.Id == id
select p).Include(m => m.AspNetRoles) .First();
model.AUser = user;
List<AspNetRole> roles = (from r in db.AspNetRoles
select r).ToList();
model.RoleList = roles;
List<ListID> ids = (from l in db.ListIDs
select l).ToList();
model.ListIDList = ids;
List<ViewerList> viewerLists = (from v in db.ViewerList
select v).ToList();
model.ViewerList = viewerLists;
}
这就是我的观点:
@using (Html.BeginForm("Edit", "Users"))
{
<div class="container containerEdit">
<div class="col-md-9">
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(m => m.AUser.Id)
<label>@Html.DisplayFor(m => m.AUser.Id)</label>
</div>
<div class="form-group">
@Html.DisplayFor(m => m.AUser.FirstName)
@Html.TextBoxFor(m => m.AUser.FirstName)
</div>
<div class="form-group">
@Html.LabelFor(m => m.AUser.PhoneNumber)
@Html.TextBoxFor(m => m.AUser.PhoneNumber)
</div>
<div class="form-group">
@Html.LabelFor(m => m.ListIDList)
@Html.DropDownListFor(n => n.ListIDList, new SelectList(Model.ListIDList, "ID", "Value"))
</div>
<div class="form-group">
@Html.LabelFor(m => m.AUser.Note1)
@Html.TextBoxFor(m => m.AUser.Note1)
</div>
<div class="form-group">
@Html.LabelFor(m => m.AUser.Note2)
@Html.TextBoxFor(m => m.AUser.Note2)
</div>
</div>
<div class="col-md-6">
<div class="form-group"> </div>
<div class="form-group">
@Html.LabelFor(m => m.AUser.LastName)
@Html.TextBoxFor(m => m.AUser.LastName)
</div>
<div class="form-group">
@Html.LabelFor(m => m.AUser.Email)
@Html.TextBoxFor(m => m.AUser.Email)
</div>
<div class="form-group">
<label>Report Viewer</label>
@Html.DropDownListFor(v => v.AUser.ViewerId, new SelectList(Model.ViewerList, "ID", "Value"))
</div>
</div>
@Html.HiddenFor(v => v.AUser.Id)
</div>
<div class="col-md-3">
@foreach (AspNetRole role in Model.RoleList)
{ @Html.CheckBox(role.Name, Model.AUser.AspNetRoles.Any(userRole => userRole.Id == role.Id)) @Html.Label(role.Name)<br /> }
</div>
</div>
<div class="col-md-8">
<input type="submit" class="btn btn-success btn-lg btnSaveEdit" value="Save" />
</div>
}
当回发发生时,我能够获取AUser的值,但我没有得到复选框中使用的Model.RoleList的任何值。 我想检查从RoleList检查哪些复选框,并将这些角色分配给用户。
知道我错过了什么吗?
提前致谢,Laziale
更新:
编辑行动:
[HttpGet]
public ActionResult Edit(int? id)
{
ManageViewModel model = new ManageViewModel();
using (var db = new DbContext())
{
AspNetUser user = (from p in db.AspNetUsers
where p.Id == id
select p).Include(m => m.AspNetRoles) .First();
model.AUser = user;
List<AspNetRole> roles = (from r in db.AspNetRoles
select r).ToList();
model.RoleList = roles;
List<ListID> ids = (from l in db.ListIDs
select l).ToList();
model.ListIDList = ids;
List<ViewerList> viewerLists = (from v in db.ViewerList
select v).ToList();
model.ViewerList = viewerLists;
}
return View(model);
}
[HttpPost]
public ActionResult Edit(ManageViewModel model)
{
AspNetUser user = model.AUser;
using (var db = new DbContext())
{
AspNetUser dbUser = (from u in db.AspNetUsers
where u.Id == model.AUser.Id
select u).First();
dbUser.FirstName = model.AUser.FirstName;
dbUser.LastName = model.AUser.LastName;
dbUser.Email = model.AUser.Email;
dbUser.PhoneNumber = model.AUser.PhoneNumber;
dbUser.Note1 = model.AUser.Note1;
dbUser.Note2 = model.AUser.Note2;
dbUser.ViewerId = model.AUser.ViewerId;
db.SaveChanges();
}
return View();
}
答案 0 :(得分:1)
问题是角色未绑定到您的视图模型。所以这就是你要做的事情:
创建新的角色视图模型:
public class RoleViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public bool Selected { get; set; }
}
修改您的ManageUserViewModel
以获得RoleViewModel
而非AspNetRole
的列表:
public class ManageUserViewModel
{
public List<RoleViewModel> Roles { get; set;}
// Your other properties
}
更改编辑以从可用角色设置Roles
属性:
public ActionResult Edit(int? id)
{
// beginning of your action
List<AspNetRole> roles = from r in db.AspNetRoles
select r;
model.Roles = new List<RoleViewModel>();
foreach(AspNetRole role in roles){
model.Roles.Add(new RoleViewModel {
Id = role.Id,
Name = role.Name,
Selected = user.AspNetRoles.Any(userRole => userRole.Id == role.Id)
});
}
// end of your action
}
将您的其他操作更改为
public ActionResult Edit(ManageViewModel model){
// beginning of your action
// remove roles from user
List<AspNetRoles> newRoles = new List<AspNetRoles>();
foreach(RoleViewModel role in model.Roles){
newRole.Add(db.AspNetRoles.Find(role.id))
}
// set your user roles from newRoles
// end of your action
}
在您的视图中,通过以下方式更改复选框部分:
@Html.EditorFor(model => model.Roles)
最后创建一个Shared/EditorTemplate/RoleViewModel.cshtml
文件:
@model RoleViewModel
@Html.HiddenFor(model => model.Id)
@Html.CheckBoxFor(model => model.Selected)
@Html.DisplayFor(model => model.Name)
答案 1 :(得分:0)
将@foreach (AspNetRole role in Model.RoleList)
替换为for
循环
重构您的AspNetRole
viewModel以包含Selected
属性
public class AspNetRole
{
public int Id { get; set; }
public string Name { get; set; }
public bool Selected { get; set; } // <- new property
}
从您AspNetRoles
ViewModel
您将在表格