我正在使用编辑器模板显示用户可以分配给的每个角色的复选框。该模型是:
public class UserModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
public IEnumerable<string> UserRoles { get; set; }
}
public class UserRoleModel
{
public IEnumerable<RoleViewModel> AllRoles { get; set; }
public UserModel user { get; set; }
public UserRoleModel()
{
this.AllRoles = Roles.GetAllRoles().Select(r => new RoleViewModel
{
Name = r
});
this.user = new UserModel();
}
}
public class RoleViewModel
{
public string Name { get; set; }
public bool Selected { get; set; }
}
控制器:
public ActionResult Create()
{
return View(new UserRoleModel());
}
[HttpPost]
public ActionResult Create(UserRoleModel model)
{
if (ModelState.IsValid)
{
MembershipCreateStatus createStatus;
Membership.CreateUser(model.user.UserName, model.user.Password, model.user.Email, null, null, true, null, out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
foreach (var r in model.AllRoles)
{
if (r.Selected)
{
Roles.AddUserToRole(model.user.UserName, r.Name);
}
}
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
}
return View(model);
}
观点:
@model BBmvc.Areas.Tools.Models.UserRoleModel
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>UserModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.user.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.user.UserName)
@Html.ValidationMessageFor(model => model.user.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.user.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.user.Email)
@Html.ValidationMessageFor(model => model.user.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.user.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.user.Password)
@Html.ValidationMessageFor(model => model.user.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.user.ConfirmPassword)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.user.ConfirmPassword)
@Html.ValidationMessageFor(model => model.user.ConfirmPassword)
</div>
<div class="editor-field">
@Html.EditorFor(x => x.AllRoles)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
和编辑器模板
@model BBmvc.Areas.Tools.Models.RoleViewModel
@Html.CheckBoxFor(x => x.Selected)
@Html.LabelFor(x => x.Selected, Model.Name)
@Html.HiddenFor(x => x.Name)
<br />
问题在于,无论是否选中任何复选框,后期操作都没有区别。它似乎不会以某种方式与模型绑定。
答案 0 :(得分:1)
您的问题来自延迟执行LINQ查询。您需要急切地初始化集合:
public class UserRoleModel
{
public IEnumerable<RoleViewModel> AllRoles { get; set; }
public UserModel user { get; set; }
public UserRoleModel()
{
this.AllRoles = Roles.GetAllRoles().Select(r => new RoleViewModel
{
Name = r
}).ToList();
this.user = new UserModel();
}
}
请注意.ToList()
来电:
this.AllRoles = Roles.GetAllRoles().Select(r => new RoleViewModel
{
Name = r
}).ToList();
这是解释。当你写:
this.AllRoles = Roles.GetAllRoles().Select(r => new RoleViewModel
{
Name = r
});
此时查询未执行。仅构建表达式树,但仅当某些内容开始迭代集合时才执行实际查询。什么开始迭代?首先是它的观点。在视图中,您使用此集合的编辑器模板:
@Html.EditorFor(x => x.AllRoles)
由于AllRoles是一个集合属性,因此ASP.NET MVC将自动迭代并呈现集合中每个元素的编辑器模板。所以这适用于正确渲染视图。
现在让我们看一下表单发布时会发生什么。您发布到Create操作并且默认模型绑定器启动。调用构造函数但是由于没有任何内容可以迭代AllRoles属性,因此此时不执行查询。实际上它会在动作中执行,并且值会丢失。
出于这个原因,我建议你不要在构造函数中初始化视图模型。在相应的控制器操作中执行此操作会更好:
public class UserRoleModel
{
public IEnumerable<RoleViewModel> AllRoles { get; set; }
public UserModel user { get; set; }
}
然后:
public ActionResult Create()
{
var model = new UserRoleModel
{
AllRoles = Roles.GetAllRoles().Select(r => new RoleViewModel
{
Name = r
}).ToList(),
user = new UserModel()
};
return View(model);
}