【问题标题】:Html.CheckBox Helper passes null to the calling controllerHtml.CheckBox Helper 将 null 传递给调用控制器
【发布时间】:2012-02-17 00:29:10
【问题描述】:

我的 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 ?我该如何解决这个问题?

提前谢谢你!

【问题讨论】:

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


    【解决方案1】:

    据我所知,当视图回发数据时,MVC 框架使用表单控件的 id 将它们与控制器操作的参数进行映射。也许这就是您无法获取正确数据的原因

    【讨论】:

    • 就是这样。我也知道...谢谢你帮我解决这个问题。
    【解决方案2】:

    改变

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

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 2022-01-25
      相关资源
      最近更新 更多