2011-01-10 14 views
6

Làm cách nào để xử lý ánh xạ nhiều đối tượng trong chế độ xem và bộ điều khiển trong ngữ cảnh usersroles?Nhiều người trong nhiều chế độ xem và bộ điều khiển asp.net mvc

tôi đã sử dụng khuôn khổ thực thể để ánh xạ POCOs tinh khiết như thế này:

public class Role 
{ 
    public int RoleId { get; set; } 
    public string RoleName { get; set; } 
    public List<User> Users { get; set; } 
} 

public class User 
{ 
    public int UserId { get; set; } 
    public List<Role> Roles { get; set; } 
} 

Theo quan điểm của tôi, tôi muốn thêm một người dùng vào một vai trò sử dụng hộp kiểm. Tôi liệt kê tất cả các vai trò sau đó chọn một vai trò để thêm người dùng vào vai trò đó. Làm cách nào để xử lý việc này?

Trả lời

15

Tôi sẽ bắt đầu bằng cách thiết kế một mô hình điểm cho kịch bản này:

public class UserRolesViewModel 
{ 
    public int UserId { get; set; } 
    public IEnumerable<RoleViewModel> Roles { get; set; } 
} 

public class RoleViewModel 
{ 
    public int RoleId { get; set; } 
    public bool InRole { get; set; } 
    public string RoleName { get; set; } 
} 

Sau đó, một vai trò điều khiển:

public class RolesController : Controller 
{ 
    public ActionResult Edit(int userId) 
    { 
     // TODO: Use a repository to fetch the roles associated to the given 
     // user id and then AutoMapper to map your model POCOs 
     // to a UserRolesViewModel 
     var model = new UserRolesViewModel 
     { 
      UserId = userId, 
      Roles = new[] 
      { 
       new RoleViewModel { RoleId = 1, InRole = false, RoleName = "Role 1" }, 
       new RoleViewModel { RoleId = 2, InRole = true, RoleName = "Role 2" }, 
       new RoleViewModel { RoleId = 3, InRole = true, RoleName = "Role 3" } 
      } 
     }; 
     return View(model); 
    } 

    [HttpPut] 
    public ActionResult Update(UserRolesViewModel model) 
    { 
     // Here you will get the view model back containing the 
     // user id and the selected roles 
     // TODO: use AutoMapper to map back to a POCO and 
     // invoke the repository to update the database 
     return RedirectToAction("Edit"); 
    } 
} 

sau đó màn hình Chỉnh sửa (~/Views/Roles/Edit.cshtml):

@model YourAppName.Models.UserRolesViewModel 
@{ 
    ViewBag.Title = "Edit user roles"; 
} 
<h2>Roles for user @Model.UserId</h2> 
@using (Html.BeginForm("Update", "Roles")) 
{ 
    @Html.HttpMethodOverride(HttpVerbs.Put) 
    @Html.HiddenFor(x => x.UserId) 
    @Html.EditorFor(x => x.Roles) 
    <input type="submit" value="update roles" /> 
} 

và cuối cùng là mẫu trình chỉnh sửa tương ứng (~/Views/Roles/EditorTemplates/RoleViewModel.cshtml):

@model YourAppName.Models.RoleViewModel 
<div> 
    @Model.RoleName 
    @Html.HiddenFor(x => x.RoleId) 
    @Html.CheckBoxFor(x => x.InRole) 
</div> 
+0

bạn có thể đưa ra alittle thêm thông tin về tuyên bố này: 'sau đó AutoMapper để lập bản đồ mô hình của bạn POCOs đến một UserRolesViewModel' –

+0

tôi đã thực hiện một câu hỏi mới về automapper: http://stackoverflow.com/questions/4653163/automapper -sử dụng –