+4  A: 

This is how i'd do it:

Firstly, you need an array of all possible roles. In the controller, i'd do something like this (i'm making some assumptions about your DAO):

ViewData["AllRoles"] = (StaffRole[])StaffRole.FindAll();

Then in your view, loop through the roles:

<% foreach (StaffRole Role in (StaffRole[])ViewData["AllRoles"]) { %>

 <p>
  <label>
   <%= Html.CheckBox("Role_"+Role.RoleId.ToString()) %>
   <%= Html.Encode(Role.RoleName) %>
  </label>
 </p>

<% } %>

Then in your POST controller, do something like this:

foreach (StaffRole Role in (StaffRole[])StaffRole.FindAll())
{
  if (Request.Params["Role_"+Role.RoleId.ToString()]=="true")
    MyStaff.Roles.Add(Role);
}
Chris
Thanks! Normally i get snarky feedback on answers posted here... good to hear.
Chris
+1  A: 

Hi the Problem with this approach is you don't really have a strongly type entity passed to the View. In this problem you need the StaffMember information and a list of all StaffRole entities. PS: I really dun like the approach of casting the list in the view : StaffRole[])ViewData["AllRoles"]

Basicly i will prefer to work with DTO.

DTO:

public StaffMemberDto
{ 
    public int StaffMemberId { get; set; }
    public IList<StaffRoleDto> AllStaffRoles { get; set;}
    public IList<StaffRoleDto> MembersRolesAttached  { get; set;}
}

public StaffRoleDto
{
    public int RoleId {get; set;}
    public string RoleName { get; set; }
}

Controller:

return View(StaffMemberDto);

So in the view you get all roles strongly typed:

foreach (var role in ViewDate.Model.AllStaffRoles)
{
    ...
}

And in the post you can send the StaffMemberDto with the good RoleDto already assigned in the view or you can do the Request trick to get the id of the checkboxes ticked.

Well in a view like this i will probably use jquery to request an addRole each time someone tick the box to add a role. It will add some ajax to your form and you will not have some postback.

alexl
This is perfect, thanks so much. I would never have thought of any of this. Just a quick FYI, you might want to edit your post to define DTO (I had to Google it) just for posterity's sake.
Martin Doms