我的观点中有类似的内容:
<input type="checkbox" value="1" name="services-for" />
<input type="checkbox" value="2" name="services-for" />
<input type="checkbox" value="3" name="services-for" />
假设用户已经检查了第1和第3个。
我的POST操作的控制器功能如下所示:
public ActionResult SaveProfile()
而不是SaveProfile(string name, List<int> servicesFor)
以及由于极端数量的字段输入(超过100)以及其中许多具有以其名称编码的值的事实(例如,name =“item-542146”)
所以我使用HttpContext.Current.Request.Form
来访问表单值。但是,HttpContext.Current.Request.Form["services-for"]
返回null,而它对正常文本输入工作正常,即不是多选。
我该怎么办?
答案 0 :(得分:2)
您最好使用FormCollection
参数,而不是从HttpContext.Current.Request
检索值,因为这仍然可以让您轻松测试您的操作方法:
public ActionResult SaveProfile(FormCollection form)
{
var servicesFor = (form["services-for"] ?? "")
.Split(',')
.Select(int.Parse);
// ...
}
请注意,如果POST表单中没有选中的输入项,form["services-for"]
可能会返回null。