Html.CheckBox Helper将null传递给调用控制器

时间:2012-02-17 00:29:11

标签: c# asp.net-mvc view html-helper

我的Html.CheckBox()View控件是否有理由将null返回到Controller? 似乎无法想出这一点,并希望得到任何帮助!

查看:

    @{ 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" })
    &nbspText exact match</span>

@{ Html.EndForm(); }

提交表单时被调用的控制器:

        public ActionResult ActionName(string code,bool boxChecked)
    {
        return View(ServiceCallGoesHere(code.Trim(),boxChecked));
    }

我无法弄清楚为什么世界上我的复选框状态没有被传递给控制器​​。为什么控制器中的boxChecked参数总是= null?我该如何解决这个问题?

提前谢谢!

2 个答案:

答案 0 :(得分:0)

据我所知,当视图回发数据时,MVC框架使用表单控件的id来映射它们与控制器动作的参数。也许这就是为什么你无法获得正确的数据

答案 1 :(得分:0)

更改

 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));
}