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

我想测试是否从我的acion methon中选中了复选框,我需要的是将复选框值从视图传递给控制器

这是我的看法

@using (Html.BeginForm("Index", "Graphe")) { 

我试着这样做:

  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 dictionnairedeparamètresconientuneentréeNullpourleparamètre«checkAct»de type non Nullable«System.Boolean»pourlaméthode«System.Web.Mvc.ActionResult Index(System.String,System.String,Boolean)»dans«Project .Controllers.GrapheController»。 Unparamètrefacultatifdoitêtreuntyperéférence,un type Nullableouêtredéclaréentantqueparamètrefacultatif。 Nomduparamètre:参数

你能帮我吗 !

如果选中了复选框,则回发值将包含[InputName]=[InputValue]forms的键值对

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

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

在标记代码中:

   

而你的行动方法签名:

 public ActionResult Index( string responsables, bool checkResp = false) 

这将起作用,因为checkResp=true复选框时,回发将包含checkResp=true ,如果未选中该复选框,则参数将默认为false。

出于某种原因,安德鲁手工创建复选框的方法对我来说不适合使用Mvc 5.相反,我使用了这个

 @Html.CheckBox("checkResp") 

创建一个与控制器配合良好的复选框。

尝试使用表单集合

  [HttpPost] public ActionResult Index(FormCollection collection) { if(!string.IsNullOrEmpty(collection["checkResp"]) { string checkResp=collection["checkResp"]; bool checkRespB=Convert.ToBoolean(checkResp); } } 

如果您希望在提交表单时由MVT控制器读取您的值,而您没有处理隐藏输入的内容。 您可以做的是将value属性添加到您的checkbox并将其设置为truefalse

MVT不会在这里将viewModel属性myCheckbox识别为true

但是如果你加了

执行此操作的脚本:

 $(document).on("click", "[type='checkbox']", function(e) { if (this.checked) { $(this).attr("value", "true"); } else { $(this).attr("value","false");} }); 

以前的解决方案都不适合我。 最后我发现该动作应编码为……

 public ActionResult Index(string MyCheck = null) 

然后,当检查时,传递的值是“on”,而不是“true”。 否则,它始终为空。

希望它有所帮助。

你应该强烈地输入你的观点。 然后你可以这样做:

 public class YourViewModel { public bool ConditionaValue { get; set; } } 

在您的视图中,您可以创建一个将绑定到此布尔值的复选框:

 @Html.CheckBoxFor(x => x.ConditionalValue) 

如果选中,则model属性为true。

对于你的直接问题,你需要将你的复选框命名为与你的动作方法参数相同的名称..它们应该是bool ..

我确实遇到了大多数解决方案的问题,因为我试图使用具有特定样式的复选框。 我需要复选框的值来将它们发送到列表中,一旦收集了值,就需要保存它。 我确实设法在一段时间后解决它。

希望它可以帮到某人。 这是下面的代码:

控制器:

  [HttpGet] public ActionResult Index(List ItemsModelList) { ItemsModelList = new List() { //example two values //checkbox 1 new Model{ CheckBoxValue = true}, //checkbox 2 new Model{ CheckBoxValue = false} }; return View(new ModelLists { List = ItemsModelList }); } [HttpPost] public ActionResult Index(ModelLists ModelLists) { //Use a break point here to watch values //Code... (save for example) return RedirectToAction("Index", "Home"); } 

型号1:

  using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace waCheckBoxWithModel.Models { public class Model { public bool CheckBoxValue { get; set; } } } 

型号2:

  using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace waCheckBoxWithModel.Models { public class ModelLists { public List List { get; set; } } } 

查看(索引):

  @{ ViewBag.Title = "Index"; @model waCheckBoxWithModel.Models.ModelLists }  @using (Html.BeginForm()) { 
@{ int cnt = Model.List.Count; int i = 0; } @foreach (var item in Model.List) { { if (cnt >= 1) { cnt--; } } @Html.Label("Example" + " " + (i + 1))
{ i++;}
}
}

对于MVC Controller方法,使用可以为null的布尔类型:

public ActionResult Index(string responsables,bool?checkResp){等}

然后,如果选中该复选框,则checkResp将为true。 如果不是,它将为空。

  public ActionResult Save(Director director) { // IsActive my model property same name give in cshtml //IsActive  
 
IsActive public ActionResult Save(Director director) { // IsValid is my Director prop same name give if(ModelState.IsValid) { DirectorVM ODirectorVM = new DirectorVM(); ODirectorVM.SaveData(director); return RedirectToAction("Display"); } return RedirectToAction("Add"); }

在复选框中设置值,如下所示:

  

将checkResp更改为您的模型中的nullable属性,如下所示:

 public Nullable checkResp{ get; set; } 

使用 ? 在checkResp之前:

 public ActionResult Index( string responsables, bool ? checkResp) { ...... } 
Responsable: Selectionnez --