2011-01-27 3 views
9

Tôi gặp một lớp học như sauCheckBoxList nhiều lựa chọn: khó khăn trong việc mô hình ràng buộc lại

public class UserRoleModel 
{ 
    public string Role { get; set; } 
    public bool UserRole { get; set; } 
} 

public UserRoleModel[] UserRoles { get; set; }


điều khiển của tôi là như sau:

public ActionResult CreateUser() 
    { 
     UserDetailsModel model = new UserDetailsModel(); 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult CreateUser(UserDetailsModel model) 
    { 

     return View(model); 
    } 

Theo quan điểm của tôi, tôi có

>@foreach (var item in Model.UserRoles)  
    { 

    name = "UserRoles"+ ".Value["+ i + "]"; 
    id= "UserRoles" + "_Value[" + i++ + "]"; 
    selected = item.UserRole ? "checked=\"checked\"" : ""; 

     <p> 
     <input type="checkbox" name="@name" id="@id" @selected value="true" /> 
     <label for="@id">@item.Role</label> 
     <input type="hidden" name="@name" value="false" /> 
     </p> 
    } 

Mặc dù giá trị được hiển thị tương ứng theo quan điểm của tôi, không có mô hình nào được ràng buộc lại cho UserRoles. Tôi đang thiếu gì hoặc có phương pháp nào tốt hơn và sạch hơn?

Trả lời

21

Những loại nội dung đó có thể đạt được một cách độc đáo với các mẫu trình chỉnh sửa. Họ cũng tránh bạn viết mã spaghetti trong quan điểm của bạn. Ví dụ:

mẫu:

public class UserDetailsModel 
{ 
    public IEnumerable<UserRoleModel> Roles { get; set; } 
} 

public class UserRoleModel 
{ 
    public string Role { get; set; } 
    public bool UserRole { get; set; } 
} 

Bộ điều khiển:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(new UserDetailsModel 
     { 
      // Fill with some dummy stuff 
      Roles = Enumerable.Range(1, 5).Select(x => new UserRoleModel 
      { 
       Role = "role " + x, 
       UserRole = false 
      }) 
     }); 
    } 

    [HttpPost] 
    public ActionResult Index(UserDetailsModel model) 
    { 
     return View(model); 
    } 
} 

View (~/Views/Home/Index.cshtml):

@model AppName.Models.UserDetailsModel 
@using (Html.BeginForm()) 
{ 
    @Html.EditorFor(x => x.Roles) 
    <input type="submit" value="OK" /> 
} 

mẫu Editor (~/Views/Home/EditorTemplates/UserRoleModel.cshtml):

@model AppName.Models.UserRoleModel 
@Html.CheckBoxFor(x => x.UserRole) 
@Html.LabelFor(x => x.Role, Model.Role) 
@Html.HiddenFor(x => x.Role) 

Bây giờ đó là những gì tôi gọi là công cụ sạch.

+0

Một điều tôi đã không nhận thấy cho đến khi tôi thực sự thử nó - @ Html.EditorFor (x => x.Roles) sẽ thực sự lặp qua bộ sưu tập. Sạch thực sự. – chris

+0

Tuyệt vời. Điều này trông giống như một giải pháp tốt đẹp cho những gì tôi đang tìm kiếm (cuối cùng). Chắc chắn sẽ thử nó vào ngày mai. Không thể tìm thấy một cái gì đó tôi đã được nội dung với điều đó cũng bị ràng buộc trở lại trên một HttpPost; có lẽ đã tìm kiếm điều sai trái. Hầu hết các giải pháp cần một mảng phụ hoặc paramter khác trong hành động HttpPost của Controller. Cảm ơn Darin. – Rodi

+1

Đã kiểm tra nó ngay bây giờ và nó cũng phù hợp với hoàn cảnh của tôi. Nhưng The I đã thay đổi 'LabelFor' để sử dụng cùng thuộc tính với' CheckBoxFor' để nhấp vào nhãn cũng sẽ kích hoạt hộp kiểm. Trong ví dụ này, nó sẽ là: '@ Html.LabelFor (x => x.UserRole, Model.Role)' – Rodi