我的Html.CheckBox()视图控件向控制器返回null是否有原因?我似乎想不出这一点,如果有任何帮助,我将不胜感激!
视图:
@{ Html.BeginForm("ActionName", "ControllerName", FormMethod.Get); }
Enter Text: @Html.TextBox("Code", string.Empty, new { id = "Code" })
<input type="submit" value="GO" />
<span style="padding-left:20px; font-size:14px" >@Html.CheckBox("exactMatch", false, new { id = "textmatches" })
 Text exact match</span>
@{ Html.EndForm(); }
提交表单时被调用的控制器:
public ActionResult ActionName(string code,bool boxChecked)
{
return View(ServiceCallGoesHere(code.Trim(),boxChecked));
}
我不明白为什么我的复选框状态没有传递给控制器。为什么控制器中的boxChecked参数始终为null?我该如何解决这个问题?
提前谢谢!
据我所知,当视图发布回数据时,MVC框架使用表单控件的id将它们与控制器的操作参数进行映射。也许这就是为什么你不能得到正确的数据
更改
public ActionResult ActionName(string code,bool boxChecked) { return View(ServiceCallGoesHere(code.Trim(),boxChecked)); }
到
> public ActionResult ActionName(string code,bool exactMatch)
{
return View(ServiceCallGoesHere(code.Trim(),exactMatch));
}