将复选框的值传递给 asp.net mvc4 中的控制器操作

编程入门 行业动态 更新时间:2024-10-27 00:32:18
本文介绍了将复选框的值传递给 asp mvc4 中的控制器操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想测试是否从我的操作方法中选中了复选框.我需要的是将复选框值从视图传递给控制器​​.

I want to test if the checkbox is checked or not from my action method. What I need is to pass checkbox value from view to controller.

这是我的观点:

@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> </table> }

我试试这个:

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 de paramètres contient une entrée Null pour leparamètre « checkAct » de type non Nullable « System.Boolean » 倒拉方法« System.Web.Mvc.ActionResult Index(System.String,System.String, Boolean) » dans « Project.Controllers.GrapheController».Un paramètre facultatif doit être un type référence, un typeNullable ou être déclaré en tant que paramètre facultatif.名称paramètre : 参数

Le dictionnaire de paramètres contient une entrée Null pour le paramètre « checkAct » de type non Nullable « System.Boolean » pour la méthode « System.Web.Mvc.ActionResult Index(System.String, System.String, Boolean) » dans « Project.Controllers.GrapheController ». Un paramètre facultatif doit être un type référence, un type Nullable ou être déclaré en tant que paramètre facultatif. Nom du paramètre : parameters

翻译:

参数字典包含一个空条目检查法"不可为空类型System.Boolean"的参数为了方法 "System.Web.Mvc.ActionResult 索引 (System.String,System.String, Boolean) in" Project.Controllers.GrapheController".一个可选参数必须是一个引用类型,一个类型可为空或被声明为可选参数.的名字参数:参数

The parameter dictionary contains a null entry for the "checkAct" parameter of non-nullable type "System.Boolean" for the method "System.Web.Mvc.ActionResult Index (System.String, System.String, Boolean) "in" Project.Controllers.GrapheController ". An optional parameter must be a reference type, a type Nullable or be declared as an optional parameter. Name of parameter: parameters

推荐答案

如果选中了复选框,则回发值将包含形式为 [InputName]=[InputValue]

If a checkbox is checked, then the postback values will contain a key-value pair of the form [InputName]=[InputValue]

如果未选中复选框,则发布的表单根本不包含对该复选框的引用.

If a checkbox is not checked, then the posted form contains no reference to the checkbox at all.

知道了这一点,以下将起作用:

Knowing this, the following will work:

在标记代码中:

<input id="responsable" name="checkResp" value="true" type="checkbox" />

以及您的操作方法签名:

And your action method signature:

public ActionResult Index(string responsables, bool checkResp = false) { //Do Something }

这将是因为检查复选框时,回发将包含 checkResp = true ,如果未选中该复选框,则参数将默认为false.

This will work because when the checkbox is checked, the postback will contain checkResp=true, and if the checkbox is not checked the parameter will default to false.

更多推荐

将复选框的值传递给 asp.net mvc4 中的控制器操作

本文发布于:2023-10-12 01:36:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483390.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   复选框   操作   net   asp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!