I have something like this on my view:
<input type="checkbox" value="1" name="services-for" />
<input type="checkbox" value="2" name="services-for" />
<input type="checkbox" value="3" name="services-for" />
Let’s say the user has checked the 1st and 3rd.
My controller function for the POST action looks like this:
public ActionResult SaveProfile()
and not SaveProfile(string name, List<int> servicesFor) and whatnot due to the extreme number of field inputs (over a 100) and the fact that many of them have values encoded in their names (for example, name=”item-542146″).
So I am using HttpContext.Current.Request.Form to access the form values. However, HttpContext.Current.Request.Form["services-for"] returns null while it works fine for normal text inputs, ie, not multi-selects.
What can I do?
You are better off using a
FormCollectionparameter as follows rather than retrieving values fromHttpContext.Current.Requestas this still allows you to easily test your action methods:Note that
form["services-for"]may return null if there are no checked input items present in the form that was POSTed.