在asp.net mvc4中将checkBox的值传递给控制器动作
我想测试复选框是否从我的acion methon被选中,我需要的是将复选框值从视图传递到控制器
这是我的看法
   @using (Html.BeginForm("Index", "Graphe"))
    {
           <table style="width: 100%;" border="1">
          <tbody>
          <tr>
          <td>Responsable:</td>
          <td><select id="Responsables" name="responsables"  ><option>Selectionnez --</option></select></td>
           <td><input id="responsable" name="checkResp" type="checkbox" /> </td>
</tr>
               <tr> <td><input type="submit" value="Afficher" id="ButtonSubmit"/></td>
                <td><input class="button" id="ButtonReset" type="button" value="Annuler"  /></td>
            </tr>
</tbody>
我试试这个:
 public ActionResult Index( string responsables, bool checkResp)
    {
        Highcharts chart = new Highcharts("chart");
        if (responsables != null)
        {
                if (checkResp)
                chart = Global();
                else
                 chart = Resp(responsables);
            }
        else
            chart = Global();
        return View(chart);
    }
,但我有这个错误:
Le dictionnaire deparamètrescontient uneentréeNull pour le leparamètre«checkAct»de type non Nullable«System.Boolean»pour laméthode«System.Web.Mvc.ActionResult索引(System.String,System.String,Boolean)»dans«项目.Controllers.GrapheController»。 Unparamètrefacultatif doitêtreun typeréférence,un type Nullableouêêdédélarlaren tant queparamètrefacultatif。 Nom duparamètre:参数
你能帮我吗 !
  如果选中复选框,则回发值将包含格式为[InputName]=[InputValue]的键值对 
如果未选中复选框,则发布的表单根本不包含该复选框。
知道这一点,以下内容将起作用:
在标记代码中:
 <input id="responsable" name="checkResp" value="true" type="checkbox" />
而你的行动方法签名:
public ActionResult Index( string responsables, bool checkResp = false)
  这将起作用,因为当复选框被选中时,回发将包含checkResp=true ,并且如果复选框未被选中,参数将默认为false。 
出于某种原因,手动创建复选框的Andrew方法不适用于使用Mvc 5的我。相反,我使用了此方法
@Html.CheckBox("checkResp")
创建一个与控制器配合使用的复选框。
尝试使用表单集合
<input id="responsable" value="True" name="checkResp" type="checkbox" /> 
[HttpPost]
public ActionResult Index(FormCollection collection)
{
     if(!string.IsNullOrEmpty(collection["checkResp"])
     {
        string checkResp=collection["checkResp"];
        bool checkRespB=Convert.ToBoolean(checkResp);
     }
}
上一篇: Pass values of checkBox to controller action in asp.net mvc4
