2011-12-22 33 views
9

Tôi thực sự đấu tranh để quấn quanh đầu tôi này:Làm thế nào tôi có thể gắn hộp kiểm để một viewmodel trong MVC3

Tôi có một UserModel và 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(); 
    } 
} 

Trong bộ điều khiển:

 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); 
    } 

Và quan điểm:

@model BBmvc.Areas.Tools.Models.UserRoleModel 

và:

@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> 

}

Trước tiên, tôi đã không thể tìm ra cách để sử dụng CheckBoxFor từ ViewModel của tôi. Nhưng nó hiển thị các tùy chọn hộp kiểm, vì vậy tôi có thể sống với nó. Nhưng tôi không thể tìm ra cách xác định hộp kiểm nào đã được kiểm tra khi biểu mẫu được đăng. Tôi cũng dường như đã phá vỡ xác nhận phía khách hàng, tôi giả sử bởi vì tôi đang sử dụng một viewModel.

Trả lời

13

Trình trợ giúp CheckBoxFor hoạt động với các thuộc tính boolean. Vì vậy, bạn có thể định nghĩa một mô hình điểm:

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

và sau đó sửa đổi các tài sản AllRoles trên mô hình quan điểm của bạn:

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(); 
    } 
} 

và trong giao diện thay vì viết foreach vòng sử dụng một mẫu biên tập:

<div class="editor-field"> 
    @Html.EditorFor(x => x.AllRoles) 
</div> 

và cuối cùng xác định mẫu trình chỉnh sửa cho loại RoleViewModel sẽ được tự động hiển thị cho từng thành phần của AllRoles bộ sưu tập (~/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 /> 

Và đó là tất cả. Bên trong bài đăng, bạn sẽ nhận được thuộc tính AllRoles được điền bằng các giá trị.

+1

Trong tác vụ bài, thuộc tính AllRoles.Selected luôn được đặt thành false, bất kể có chọn hộp kiểm nào hay không. – Rob

0

Như Rob được đề cập trong câu trả lời trước đó, tất cả thuộc tính đã chọn sẽ là Sai. Tôi đã sử dụng đoạn mã này để trang trải.

AllRoles = Roles.GetAllRoles().Select(r => new RoleViewModel() 
{ 
    Name = r, 
    Selected = Roles.GetRolesForUser(uvm.UserProfile.UserName).Contains(r) ? true : false 
}); 
Các vấn đề liên quan