How to use checklistbox to update roles in account in mvc3

161 Views Asked by At

I have any Model

public class Account
    {
        [Key]
        public int AccountID { get; set; }

        public string UserName { get; set; }

        public string Password { get; set; }

        public string FullName { get; set; }
}

And

public class Role
{
    [Key]
    public int RoleID { get; set; }
    public string RoleKey { get; set; }
    public string RoleName { get; set; }

}

And

public class AccountRole
{
    [Key]
    public int ID { get; set; }
    public int AccountID { get; set; }
    public string Role { get; set; }

}

I want to use one view update Account and list role to list checkbox. When I checked in checkbox then role to insert in to AccountRole.

Please tell me how to create my View.

1

There are 1 best solutions below

0
On

Create a holder for a Role + a boolean IsSelected

public class SelectionRoles
{
   public bool IsSelected { get; set;}
   public Role OneRole { get; set; }
}

use this in your ViewModel as a List, pass it to your View and use it for display, when you submit your form you can check in your controller which roles are checked by looking in your ViewModel at the IsSelected boolean for each record in your SelectionRole List.