views:

36

answers:

2

I have an mvc app where I want to assign a user to a profile. I have two multiline select lists (i.e <select size="10"></select>). One for profiles the user is part of and one for the available profiles the user can become part of.

I am using JavaScript with two buttons to move the items between the two lists. When the form submits the Request.Form object only knows about options that are selected in either list but I want to know all the items that are in the 'profiles the user is part of' list.

What can I do to acheive this? I was thinking of using a hidden field and placing the values in the field when they are moved to the 'profiles the user is part of' list. (i.e. <input type="hidden" name="user_profiles" values="3,8,12">).

What do you think? Is there a better way to do this? Thanks.

+1  A: 

You could have a script to select everything in the list on the submit event.

harriyott
Never thought of that, thanks.
modernzombie
A: 

Pass in the FormCollection to the action. Then update the model and store the data.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveProfile(FormCollection collection){

  // collection is a key based collection so pass the key or the control name to get   
  // its value 

  Profile p = new Profile();
  p.UserId = collection["UserId"];
  p.Name = collection["Name"];
  p.Birthdate = collection["Birthdate"];
  p.ProfileOption = collection["ProfileOption"];

  // DB logic should be done in a service but this is a very simplified example
  profileRepository.Update(p); 

  return View();
}
gmcalab
@gmcalab, this provides the same result as Request.Form["key"], it only gives me the selected options in the list, not all the options in the list (regardless of whether they are selected).
modernzombie