asp.net-mvc-3 – 如何将复选框绑定到mvc3中的viewmodel
发布时间:2020-05-22 21:39:14 所属栏目:asp.Net 来源:互联网
导读:我真的很努力地围绕着这个: 我有一个UserModel和一个UserRoleModel: public class UserModel{ [Required] [Display(Name = User name)] public string UserName { get; set; } [Required] [DataType(DataType.E
|
我真的很努力地围绕着这个: 我有一个UserModel和一个UserRoleModel: 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<string> AllRoles { get; set; }
public UserModel user { get; set; }
public UserRoleModel()
{
this.AllRoles = Roles.GetAllRoles();
this.user = new UserModel();
}
}
在控制器中: 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,true,out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
foreach (var r in model.AllRoles)
{
Roles.AddUserToRole(model.user.UserName,r);
}
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">
@foreach (var r in @Model.AllRoles)
{
@Html.CheckBox(r,false)
@Html.Label(r)
<br />
}
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
} 首先,我无法从我的viewModel中找出如何使用CheckBoxFor.但它显示复选框选项,所以我可以与它一起生活.但是,我无法确定如何确定哪个复选框在表单发布时被检查.我也似乎破坏了客户端验证,我假设因为我使用viewModel. 解决方法CheckBoxFor助手以布尔属性运行.所以你可以定义一个视图模型:public class RoleViewModel
{
public string Name { get; set; }
public bool Selected { get; set; }
}
然后在视图模型上修改AllRoles属性: 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();
}
}
而在视图中而不是编写foreach循环使用编辑器模板: <div class="editor-field">
@Html.EditorFor(x => x.AllRoles)
</div>
最后定义一个RoleViewModel类型的编辑器模板,它将自动为AllRoles集合的每个元素呈现(/ Views / Shared / EditorTemplates / RoleViewModel.cshtml) @model RoleViewModel @Html.CheckBoxFor(x => x.Selected) @Html.LabelFor(x => x.Selected,Model.Name) @Html.HiddenFor(x => x.Name) <br /> 就这样.在Post操作中,您将获得使用值填充的AllRoles属性. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何以编程方式在ASP.NET MVC中显示/隐藏Razor View中的一个
- 如何检测ASP.NET应用程序中的SqlServer连接泄漏?
- asp.net – 有没有办法每天在.Net Web应用程序中运行一个进
- asp.net-mvc – webgrid中的Mvc 3 texbox(razor)
- asp.net-mvc – 将JSON格式的DateTime传递给ASP.NET MVC
- 如何使用ASP.NET获取访问者位置(国家,州和城市)
- asp.net CustomValidator从不触发OnServerValidate
- asp.net-mvc – 如何在MVC 4 API中打印JSON脚本
- ASP.NET:这个HttpModule有什么作用? System.ServiceModel
- 从ASP.NET 3.5应用程序在IFRAME中运行GWT应用程序(包括Appl
推荐文章
站长推荐
- asp.net – 如何从相同的解决方案的MVC项目调试W
- asp.net – Orchard CMS如何进行日志记录?
- asp.net-mvc – 一种在ASP.NET MVC中排除操作过滤
- asp.net – ADO.NET实体数据模型缺少Visual Stud
- asp.net-mvc-4 – 后退点击刷新页面 – MVC 4
- asp.net – 实体框架到多个数据库(同一架构)在运
- asp.net – Service Fabric中的.NET Core RC2
- asp.net – 动态更改GridView项目模板
- asp.net-mvc – 使用jQuery ajax / load提交数组
- asp.net-mvc – ASP.NET MVC角色授权
热点阅读
