· 8 years ago · Jul 28, 2018, 09:30 AM
1checkbox does not retain its state
2public ActionResult Edit(int id)
3 {
4 accounts accounts = db.accounts
5 .Include(i => i.roles_accounts)
6 .Where(i => i.id_account == id)
7 .Single();
8 PopulateAssignedRoleData(accounts);
9 return View(accounts);
10 }
11
12 // populate Assigned RoleDATA pour afficher les checkbox
13 private void PopulateAssignedRoleData(accounts account)
14 {
15 //Get all role
16 var allRole =db.roles;
17 //For each role, the code checks if the role exists in the property of accountRole
18 // To create effective search when checking if a role is assigned to the account,
19 // assigned roles in are put into a collection HashSet
20 var accountRoles = new HashSet<int>(account.roles_accounts.Select(r => r.id_account_role));
21 var viewModel = new List<AssignedRoleData>();
22 // Property Assigned role of which is allocated account is set to true.
23 //The view will use this property to determine
24 //what check boxes to be displayed as selected.
25 //Finally, the list is passed to the view in a ViewBag
26 foreach (var role in allRole)
27 {
28 viewModel.Add(new AssignedRoleData
29 {
30 RoleId = role.id_role,
31 Name = role.name,
32 Assigned = accountRoles.Contains(role.id_role)
33 });
34 }
35 ViewBag.roles = viewModel;
36 }
37
38 //
39 // POST: /Account/Edit/5
40
41 [HttpPost]
42 public ActionResult Edit(int id, FormCollection formCollection, string [] selectedRoles)
43 {
44
45
46 var accountsToUpdate = db.accounts
47 .Include(i => i.roles_accounts)
48 .Where(i => i.id_account == id)
49 .Single();
50 if (TryUpdateModel(accountsToUpdate, "", null, new string[] { "roles_accounts" }))
51 {
52 try
53 {
54 if (String.IsNullOrWhiteSpace(accountsToUpdate.login))
55 {
56 accountsToUpdate.roles_accounts = null;
57 }
58 UpdateAccountRole(selectedRoles, accountsToUpdate);
59 db.Entry(accountsToUpdate).State = EntityState.Modified;
60 db.SaveChanges();
61 return RedirectToAction("Index");
62 }
63
64 catch (DataException)
65 {
66 ModelState.AddModelError("", "Unable to save change");
67 }
68 }
69 PopulateAssignedRoleData(accountsToUpdate);
70 return View(accountsToUpdate);
71 }
72
73 // update AccountRole (liste of checkbox)
74 private void UpdateAccountRole(string[] selectedRoles, accounts accountToUpdate)
75 {
76 if (selectedRoles == null)
77 {
78
79 accountToUpdate.roles_accounts=new List<roles_accounts>();
80 return;
81 }
82 var selectedRolesHS = new HashSet<string>(selectedRoles);
83 var accountsRoles = new HashSet<int>
84 (accountToUpdate.roles_accounts.Select(r => r.id_account_role));
85
86 foreach(var role in db.roles_accounts)
87 {
88 if( selectedRolesHS.Contains(role.id_account_role.ToString()))
89 {
90 if(!accountsRoles.Contains(role.id_account_role))
91 {
92 accountToUpdate.roles_accounts.Add(role);
93 }
94 }
95 else
96 {
97 if (accountsRoles.Contains(role.id_account_role))
98 {
99 accountToUpdate.roles_accounts.Remove(role);
100 }
101 }
102 }
103 }
104
105public class AssignedRoleData
106 {
107 public int RoleId { get; set; }
108 public string Name { get; set; }
109 public bool Assigned { get; set; }
110
111<div class="editor-field">
112 <table>
113 <tr>
114 @{
115 int cnt = 0;
116
117 List<App_ERP1.ViewModels.AssignedRoleData> roles=ViewBag.roles;
118
119 foreach (var role in roles) {
120 if (cnt++ % 3 == 0) {
121 @: </tr> <tr>
122 }
123 @: <td>
124 <input type="checkbox"
125 name="selectedRoles"
126 value="@role.RoleId"
127 @(Html.Raw(role.Assigned ? "checked="checked"" : "")) />
128 @role.RoleId @: @role.Name
129 @:</td>
130 }
131 @: </tr>
132 }
133
134 }
135}
136
137for (i = 0; i< roles.Count, i++) {
138 if (cnt++ % 3 == 0) {
139 @: </tr> <tr>
140 }
141 @: <td>
142 <input type="checkbox"
143 name="selectedRoles[" + i + "]"
144 value="@role.RoleId"
145 @(Html.Raw(role.Assigned ? "checked="checked"" : "")) />
146 @role.RoleId @: @role.Name
147 @:</td>
148 }
149 @: </tr>