2011-01-10 18 views
6

में से कई लोगों को users और roles के संदर्भ में दृश्य और नियंत्रक में कई ऑब्जेक्ट मैपिंग में कितने संभालते हैं?एएसपीनेट एमवीसी व्यू और कंट्रोलर

मैं इस तरह शुद्ध Pocos को मैप करने के इकाई की रूपरेखा का प्रयोग किया:

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

मेरे विचार में, मैं एक भूमिका आइकॉन का उपयोग करके करने के लिए एक उपयोगकर्ता जोड़ना चाहते हैं। मैं सभी भूमिकाओं को सूचीबद्ध करता हूं, फिर उपयोगकर्ता को उस भूमिका में जोड़ने के लिए एक की जांच करता हूं। इससे मैं कैसे निपटूं?

उत्तर

15

मैं इस स्थिति के लिए एक दृश्य के मॉडल के डिजाइन से शुरू होगा:

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

फिर एक भूमिकाओं नियंत्रक:

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

उसके बाद दृश्य संपादित करें (~/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" /> 
} 

और अंत में संबंधित संपादक टेम्पलेट (~/Views/Roles/EditorTemplates/RoleViewModel.cshtml):

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

आप इस कथन पर अधिक जानकारी के alittle दे सका: 'तो AutoMapper एक UserRolesViewModel' –

+0

करने के लिए अपने मॉडल Pocos मैप करने के लिए मैं automapper के बारे में एक नया सवाल किया: http://stackoverflow.com/questions/4653163/automapper -उपयोग –

संबंधित मुद्दे