asp.net-mvc - 如何使用 @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”] 返回一个“真,假”值,而不是复选框的实际选中值。我似乎无法获得表格来收集复选框的实际状态。
我尝试了其他选择,但似乎都没有奏效。任何帮助是极大的赞赏!
解决方案
推荐阅读
- java - 结构搜索 IntelliJ IDEA
- javascript - 数组有时会重新创建,有时会修改
- node.js - STM MP157C DK2 Node.js 可能吗?
- c# - 我找到 ConfigurationSettings 但不是 configurationManager
- r - 在R中的多对列中查找重复元素
- javascript - 验证 Dropbox 以确保用户选择了一个值
- php - 将 jQuery var 发布到 PHP 文件并设置 cookie
- ios - 视觉工作室中的未知个人资料
- jenkins-pipeline - 丢弃多分支管道作业中的旧构建,并不会真正从服务器中删除构建
- activemq-artemis - ActiveMQ 核心桥接集群