This question is an exact duplicate of:
- MVC checkbox for users in group 1 answer
I have database with table Users and Roles and I need view that shows me All roles available and checked the ones the user have. I already have method that shows me all roles available and method that shows me the ones user have, but I dont really know how to implement that in view, how to make those checkboxes actually checked. Thanks very much for help
Table roles
Method for all roles available
Method for selected roles
Model
public class UserRoleModel
{
public int ID { get; set; }
public int? UserID { get; set; }
public UserRoleModel()
{
ListRole = new List<RoleVM>();
}
public string Name { get; set; }
public List<RoleVM> ListRole { get; set; }
public List<RoleVM> UserRole { get; set; }
}
public class RoleVM
{
public int ID { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
}
}
View with checkboxes
@for(int i = 0; i < Model.ListRole.Count; i++)
{
@Html.HiddenFor(m => m.ListRole[i].ID)
@Html.CheckBoxFor(m => m.ListRole[i].IsSelected, new { @checked = "checked" });
@Html.LabelFor(m => m.ListRole[i].IsSelected, Model.ListRole[i].Name)
}
Aucun commentaire:
Enregistrer un commentaire