Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 2.67 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. .NET MVC 4 Strongly typed ViewModel containing Strongly typed Model with EditorFor and EditorTemplate partial view not binding
  2. public class RoleVM {
  3.  
  4. [Required]
  5. [Display(Name = "Name of the Role")]
  6. public string Role {get; set;}
  7.  
  8. public IEnumerable<RolePermission> permissions { get; set; }
  9.  
  10. }
  11.        
  12. public class RolePermission {
  13.  
  14. public int id;
  15.  
  16. public bool permission_value;
  17.  
  18. public string name { get; set; }
  19.  
  20. }
  21.        
  22. public ActionResult Create() {
  23.  
  24.         RoleVM role_vm = new RoleVM();
  25.  
  26.         var allpermissions = from p
  27.         in permission_repo.GetPermissions()
  28.         select p;
  29.  
  30.         role_vm.permissions = from p
  31.         in allpermissions
  32.         select new RolePermission
  33.         { name = p.name, id = p.PermissionId, permission_value = false };
  34.  
  35.         return View(role_vm);
  36.     }
  37.        
  38. @model RoleVM
  39. @using (Html.BeginForm("Create", "Role",
  40. FormMethod.Post, new { @class = "permission_form" }))
  41. {
  42. @Html.ValidationSummary(true)
  43.  
  44. <fieldset>
  45.     <legend>RoleVM</legend>
  46.  
  47.     <div class="form-item">
  48.         @Html.LabelFor(model => model.Role)
  49.  
  50.         @Html.EditorFor(model => model.Role)
  51.         @Html.ValidationMessageFor(model => model.Role)
  52.     </div>
  53.  
  54.    @Html.EditorFor(model => model.permissions)
  55.  
  56.     <p>
  57.         <input class="submit-btn" type="submit" value="Create" />
  58.     </p>
  59. </fieldset>
  60. }
  61.        
  62. @model RolePermission
  63.  
  64. <div class="form-item">
  65.       @Html.HiddenFor(modelItem => modelItem.id)
  66.       @Html.LabelFor(modelItem => modelItem.permission_value, Model.name)
  67.       @Html.CheckBoxFor(modelItem => modelItem.permission_value)
  68. </div>
  69.        
  70. <div class="form-item">
  71.  
  72.       <input data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." id="permissions_2__id" name="permissions[2].id" type="hidden" value="3" />
  73.  
  74.       <label for="permissions_2__permission_value">Role-Edit</label>
  75.       <input data-val="true" data-val-required="The Boolean field is required."  id="permissions_2__permission_value" name="permissions[2].permission_value"
  76.  type="checkbox" value="true" /><input name="permissions[2].permission_value"  type="hidden" value="false" />
  77.  
  78. </div>
  79.        
  80. [HttpPost]
  81.     public ActionResult Create(RoleVM rolevm)
  82.     {
  83.  
  84.         //In here rolevm.role is populated based on the textbox input
  85.         //However rolevm.permissions is there with the correct
  86.         //number of items, but the values all are not binded
  87.         // id is null, name is empty, and permission_value is false
  88.         // regardless of which checkboxes were checked
  89.  
  90.  
  91.         return RedirectToAction("Index");
  92.     }
  93.        
  94. public class RolePermission {
  95.  
  96. public int id { get; set; }
  97.  
  98. public bool permission_value { get; set; }
  99.  
  100. public string name { get; set; }
  101.  
  102. }