0

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

enter image description here

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.

shad0wec
  • 335
  • 1
  • 3
  • 15
  • Do you know what the roles are and will they be fixed? – CodePhobia Sep 10 '15 at 10:58
  • 4
    Having a User model with 4 bool properties is not very flexible - what happens if you add or remove roles later. I suspect something like [this question/answer](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) would be more suitable –  Sep 10 '15 at 10:58
  • Nice One @StephenMuecke! Timeless answer! – CodePhobia Sep 10 '15 at 11:04
  • I did everything in that question/answer and its way better, but it only shows the ones User has and is not checked :/ I need all of roles to be shown and the ones have to be checked – shad0wec Sep 10 '15 at 12:03
  • @shad0wec, In the linked answer, you need to first populate the `Roles` property with all roles from the database, then based on the specific users roles, you set the `IsSelected` property as appropriate. If your having problems, ask a new question with the code you have tried and we will help you. –  Sep 10 '15 at 12:18
  • @Stephen Muecke will do. give me a minute – shad0wec Sep 10 '15 at 12:23
  • [http://stackoverflow.com/questions/32502061/mvc-checkbox-for-users-in-group] here it is – shad0wec Sep 10 '15 at 12:35

0 Answers0