When I'm trying to get values in Post the values of checkboxes are set to NULL when I don't check then in order (1, 2, 3, etc).
I need to select any of them in no order (i.e. 4, 5).
MODEL:
public class AssignUsersViewModel
{
[Display(Name = "Check to select")]
public bool Select { get; set; }
public int Id { get; set; }
[Display(Name = "App. Username")]
public string UserName { get; set; }
[Required]
public string GivenName { get; set; }
[Required]
public string Surname { get; set; }
[Display(Name = "Roles")]
public IList<Roles> Roles { get; set; }
}
public class AssignUsersAddModel
{
public bool Select { get; set; }
public int Id { get; set; }
public IEnumerable<SelectedRoles> selectedRoles { get; set; }
}
public class SelectedRoles
{
public string Name { get; set; }
}
CSHTML:
@model IList<AspNetIdentity2DRH.Models.AssignUsersViewModel>
@using (Html.BeginForm("UsersAddToApp", "UsersAdmin", FormMethod.Post))
{
@Html.AntiForgeryToken()
<table class="table">
<tr>
<th>Check for add</th>
<th>Username</th>
<th>Givenname</th>
<th>Surename</th>
<th>Roles</th>
</tr>
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
@Html.CheckBoxFor(x => x[i].Select)
@Html.HiddenFor(x => x[i].Id)
</td>
<td>
@Html.DisplayFor(x => x[i].UserName)
</td>
<td>
@Html.DisplayFor(x => x[i].GivenName)
</td>
<td>
@Html.DisplayFor(x => x[i].Surname)
</td>
<td>
<div class="row">
<div class="form-group">
@for (int j = 0; j < Model[i].Roles.Count(); j++)
{
<div class="col-sm-4">
<input type="checkbox" name="[@i.ToString()].selectedRoles[@j.ToString()].Name" value="@Model[i].Roles[j].Name" class="checkbox-inline" />
@Html.Label(Model[i].Roles[j].Name, new { @class = "control-label", @data_toggle = "tooltip", @data_placement = "top", @data_original_title = Model[i].Roles[j].Description })
</div>
}
</div>
</div>
</td>
</tr>
}
</table>
<p>
<input type="submit" value="Add existing user" class="btn btn-primary" />
<input type="button" value="Cancel" onclick="window.location.href = '@Url.Action("UsersIndex", "UsersAdmin")';" class="btn btn-cancel" />
</p>
}
CONTROLLER:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UsersAddToApp(List<AssignUsersAddModel> model)
{
if (ModelState.IsValid)
{
foreach (AssignUsersAddModel item in model)
{
if (item.Select)
{
using (DbContextTransaction dbtrans = db.Database.BeginTransaction())
{
try
{
int appId = (int)Session["ApplicationId"];
Users user = UserManager.FindById(item.Id);
db.ApplicationUsers.Add(new ApplicationUsers { ApplicationId = appId, UserId = user.Id });
db.SaveChanges();
foreach (SelectedRoles RolesItem in item.selectedRoles)
{
int roleId = db.Roles.Where(r => r.Name == RolesItem.Name).Select(r => r.Id).FirstOrDefault();
db.UserApplicationRoles.Add(new UserApplicationRoles { ApplicationId = appId, UserId = user.Id, RoleId = roleId });
db.SaveChanges();
}
dbtrans.Commit();
}
catch (Exception)
{
dbtrans.Rollback();
}
}
}
}
return RedirectToAction("UsersAddToApp");
}
ModelState.AddModelError("", "An error has occurred. Contact system administrator.");
return RedirectToAction("UsersAddToApp");
}
The problem is when I select checkboxes (all except the first, or the last o one in the middle, the line:
foreach (SelectedRoles RolesItem in item.selectedRoles)
Sends item.selectedRoles is null.
How I could do this right?