I know this question might be answered already, but the problem is that I can not really find the proper solution. So I have one MVC app with a database of Users and Roles. I need to create view where will be Users ID and his Roles in checkboxes, if he has the role, the checkbox will be obviously checked. I hope you got me. By the way the Roles and Users tables are separated, only UserID is same.
Roles in database
The model
public class UserRoleModel
{
public int ID { get; set; }
public int UserID { get; set; }
public List<RoleModel> ListRole {get; set;}
public bool Administrator { get; set; }
public bool Gestor { get; set; }
public bool Supervisor { get; set; }
public bool SecurityManager { get; set; }
}
public class RoleModel
{
public string Name {get; set;}
}
The controller ( which is probably completly wrong, by the way Im only looking for id 1023 right now )
public ActionResult Role(/*string roleName, string username,*/ int id)
{
//Roles.IsUserInRole(username, roleName);
UsersRole u = _context.UsersRoles.Find(/*x => x.UserID == id*/1023);
UserRoleModel model = new UserRoleModel();
//model.UserID = u.UserID;
//model.ID = u.ID;
model.ListRole = _context.UsersRoles.Where(c => c.UserID == 1023).Select(c => new RoleModel { Name = c.Role}).ToList();
return View(model)
}
And here is the part of the view, I need to use foreach, I know that and this only gives me Text in which role UserID 1023 is. so it only writes "Administrator" and nothing else.
@foreach(var item in Model.ListRole)
{
@Html.EditorFor(x => x.ListRole)
}
Thanks for any help and yes Im quite new to MVC and programming at all.