Reputation: 25
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?
Upvotes: 2
Views: 862
Reputation: 7514
You are better off using a FormCollection
parameter as follows rather than retrieving values from HttpContext.Current.Request
as this still allows you to easily test your action methods:
public ActionResult SaveProfile(FormCollection form)
{
var servicesFor = (form["services-for"] ?? "")
.Split(',')
.Select(int.Parse);
// ...
}
Note that form["services-for"]
may return null if there are no checked input items present in the form that was POSTed.
Upvotes: 2