gpt4 book ai didi

asp.net-mvc-3 - 如何将复选框绑定(bind)到 mvc3 中的 View 模型

转载 作者:行者123 更新时间:2023-12-04 03:33:48 26 4
gpt4 key购买 nike

我真的很难解决这个问题:

我有一个 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();
}
}

在 Controller 中:
        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)
{
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 助手使用 bool 属性进行操作。所以你可以定义一个 View 模型:

public class RoleViewModel
{
public string Name { get; set; }
public bool Selected { get; set; }
}

然后修改 View 模型上的 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();
}
}

并且在 View 中而不是写 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-3 - 如何将复选框绑定(bind)到 mvc3 中的 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8610210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com