首页 > 解决方案 > 如何使用 @Html.CheckBox 将 CheckBox 值从视图传递到控制器?

问题描述

我需要在我的视图上显示 6 种类型的测试,并允许用户检查他们想要完成的尽可能多的测试。然后我需要将他们的响应发送到我的控制器,以便我可以使用实体框架将该信息添加到数据库中。

我的观点:

@using (Html.BeginForm("Quote", "Customers", FormMethod.Post))
{
    <label for="test1"> BP</label>
    @Html.CheckBox("test1", true)<br />

    <label for="test2"> DS</label>
    @Html.CheckBox("test2")<br />

    <label for="test3"> IS</label>
    @Html.CheckBox("test3")<br />

    <label for="test4" class="iput">PS</label>
    @Html.CheckBox("test4")<br />

    <label for="test5">PF</label>
    @Html.CheckBox("test5")<br />

    <label for="test6">CS</label>
    @Html.CheckBox("test6")<br />

    <input type="submit" value="Begin Quote" class="btn btn-default" style="padding-left: 20px" />
} 

和我的控制器方法:

[HttpPost]
        public ActionResult Quote(FormCollection collection )
        {

            if (!string.IsNullOrEmpty(collection["test1"]))
               {
                   string checkResp = collection["test1"];
                   bool checkRespB = Convert.ToBoolean(checkResp);
               }


            return View("Index");
        }

我在调试时遇到了一个问题...集合 [“test1”] 返回一个“真,假”值,而不是复选框的实际选中值。我似乎无法获得表格来收集复选框的实际状态。

我尝试了其他选择,但似乎都没有奏效。任何帮助是极大的赞赏!

标签: asp.net-mvcrazorcheckboxhtml-helper

解决方案


推荐阅读