Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将复选框的值传递给asp.net mvc4中的控制器操作_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4_Checkbox - Fatal编程技术网

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

C# 将复选框的值传递给asp.net mvc4中的控制器操作,c#,asp.net,asp.net-mvc,asp.net-mvc-4,checkbox,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Checkbox,我想测试我的操作方法是否选中了复选框,我需要的是将复选框值从视图传递给控制器 我的看法是: @using (Html.BeginForm("Index", "Graphe")) { <table style="width: 100%;" border="1"> <tbody> <tr> <td>Responsable:</td>

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

我的看法是:

   @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>
但我收到了这个错误:

参数的使用说明继续输入空值,输入类型不可为空的«System.Boolean»输入方法«System.Web.Mvc.ActionResult索引(System.String,System.String,Boolean)»dans«Project.Controllers.GrapherController»。非参数facultatif doiètètètètètètètètètètètètèt référence,非类型Nullable ou tre d。 参数名称:参数


你能帮帮我吗

您应该大力键入您的视图。然后你可以这样做:

public class YourViewModel {
    public bool ConditionaValue { get; set; }
}
在您的视图中,您可以创建一个将绑定到此布尔值的复选框:

@Html.CheckBoxFor(x => x.ConditionalValue)
如果选中该选项,则“模型”属性将为true


但是为了你眼前的问题。。您需要将复选框命名为与操作方法参数相同的名称。。它们应该是
bool

尝试使用表单集合

<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);
     }

}

[HttpPost]
公共行动结果索引(FormCollection集合)
{
如果(!string.IsNullOrEmpty(集合[“checkResp”])
{
字符串checkResp=集合[“checkResp”];
bool checkRespB=转换为oolean(checkResp);
}
}

如果选中复选框,则回发值将包含一个键值对,其形式为
[InputName]=[InputValue]

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

了解了这一点,以下措施将起作用:

在标记代码中:

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

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

由于某些原因,手动创建复选框的方法在使用Mvc 5时对我不起作用。相反,我使用了此方法

@Html.CheckBox("checkResp")

创建一个复选框,该复选框可以很好地与控制器配合。

如果您希望MVT控制器在提交表单时读取您的值,而您不需要处理隐藏的输入。您可以做的是将
value
属性添加到
复选框中,并将其设置为
true
false

MVT不会在此处将viewModel属性
myCheckbox
识别为true

但是如果你加上

执行此操作的脚本:

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

对于MVC控制器方法,请使用可为空的布尔类型:

公共行动结果索引(字符串责任、bool?checkResp) { 等 }


然后,如果选中该复选框,则checkResp将为true。如果未选中,则为null。

以前的解决方案都不适用于我。 最后,我发现操作应编码为:

public ActionResult Index(string MyCheck = null)
然后,当检查时,传递的值为“开”,而不是“真”。否则,它总是空的

希望它能帮助别人!


<form action="Save" method="post">
 IsActive <input type="checkbox" id="IsActive" checked="checked" value="true" name="IsActive"  />

 </form>

 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");
        }
活跃的 公共行动结果保存(主管) { //IsValid是我的导演道具的名字吗 if(ModelState.IsValid) { DirectorVM ODirectorVM=新的DirectorVM(); ODirectorVM.SaveData(director); 返回重定向到操作(“显示”); } 返回重定向操作(“添加”); }
公共行动结果保存(主管)
{
//IsActive我的模型属性与cshtml中给出的名称相同

//IsActive在复选框中设置如下值:

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

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

希望它能帮助别人。 下面是代码:

控制器:

    [HttpGet]
    public ActionResult Index(List<Model> ItemsModelList)
    {

        ItemsModelList = new List<Model>()
        {                
            //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");

    }
模式2:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace waCheckBoxWithModel.Models
    {
        public class ModelLists
{

    public List<Model> List { get; set; }

}
    }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
命名空间waCheckBoxWithModel.Models
{
公共类模型列表
{
公共列表{get;set;}
}
}
视图(索引):

@{
ViewBag.Title=“Index”;
@模型waCheckBoxWithModel.Models.ModelList
}
.复选框{
显示:块;
位置:相对位置;
边缘底部:12px;
光标:指针;
字体大小:22px;
-webkit用户选择:无;
-moz用户选择:无;
-ms用户选择:无;
用户选择:无;
}
/*隐藏默认复选框*/
.复选框输入{
位置:绝对位置;
不透明度:0;
光标:指针;
}
/*对勾*/
.对号{
位置:绝对位置;
排名:0;
左:0;
高度:25px;
宽度:25px;
背景:#fff;
边界半径:4px;
边框宽度:1px;
盒影:嵌入0px 0px 10px#ccc;
}
/*在鼠标上方更改背景颜色*/
.复选框:悬停输入~.复选标记{
/*背景色:#ccc*/
}
/*背景效应*/
.复选框输入:选中~.复选标记{
背景色:#fff;
}
/*选中标记(未选中时隐藏)*/
.勾选:之后{
内容:“;
位置:绝对位置;
显示:无;
}
/*显示选中标记(选中)*/
.复选框输入:选中~.复选标记:之后{
显示:块;
}
/*样式复选标记*/
.checkBox.checkmark:after{
左:9px;
顶部:7px;
宽度:5px;
高度:10px;
边框:实心#1F4788;
边框宽度:0 2px 2px 0;
-webkit变换:旋转(45度);
-ms变换:旋转(45度);
变换:旋转(45度);
}
@使用(Html.BeginForm())
{
@{
int cnt=Model.List.Count;
int i=0;
}
@foreach(Model.List中的var项)
{
{
如果(cnt>=1)
{cnt--;}
}
@Label(“示例“+”+(i+1))

@CheckBoxFor(m=>Model.List[i].CheckBoxValue) {i++;}
}
<input id="responsable" name="checkResp" value="true" type="checkbox" />
public Nullable<bool> checkResp{ get; set; }
public ActionResult Index( string responsables, bool ? checkResp)
{
 ......
}
    [HttpGet]
    public ActionResult Index(List<Model> ItemsModelList)
    {

        ItemsModelList = new List<Model>()
        {                
            //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");

    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace waCheckBoxWithModel.Models
    {
        public class Model
{

    public bool CheckBoxValue { get; set; }

}
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace waCheckBoxWithModel.Models
    {
        public class ModelLists
{

    public List<Model> List { get; set; }

}
    }
    @{
ViewBag.Title = "Index";

@model waCheckBoxWithModel.Models.ModelLists
    }
    <style>

.checkBox {
    display: block;
    position: relative;
    margin-bottom: 12px;
    cursor: pointer;
    font-size: 22px;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

    /* hide default checkbox*/
    .checkBox input {
        position: absolute;
        opacity: 0;
        cursor: pointer;
    }

/* checkmark */
.checkmark {
    position: absolute;
    top: 0;
    left: 0;
    height: 25px;
    width: 25px;
    background: #fff;
    border-radius: 4px;
    border-width: 1px;
    box-shadow: inset 0px 0px 10px #ccc;
}

/* On mouse-over change backgroundcolor */
.checkBox:hover input ~ .checkmark {
    /*background-color: #ccc;*/
}

/* background effect */
.checkBox input:checked ~ .checkmark {
    background-color: #fff;
}

/* checkmark (hide when not checked) */
.checkmark:after {
    content: "";
    position: absolute;
    display: none;
}

/* show checkmark (checked) */
.checkBox input:checked ~ .checkmark:after {
    display: block;
}

/* Style checkmark */
.checkBox .checkmark:after {
    left: 9px;
    top: 7px;
    width: 5px;
    height: 10px;
    border: solid #1F4788;
    border-width: 0 2px 2px 0;
    -webkit-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transform: rotate(45deg);
}
   </style>

    @using (Html.BeginForm())
    {

    <div>

@{
    int cnt = Model.List.Count;
    int i = 0;

}

@foreach (var item in Model.List)
{

    {
        if (cnt >= 1)
        { cnt--; }
    }

    @Html.Label("Example" + " " + (i + 1))

    <br />

    <label class="checkBox">
        @Html.CheckBoxFor(m => Model.List[i].CheckBoxValue)
        <span class="checkmark"></span>
    </label>

    { i++;}

    <br />

}

<br />
<input type="submit" value="Go to Post Index" />    

    </div>

    }
public class UsersViewModel
{
    public bool IsAdmin { get; set; }
    public bool ManageFiles { get; set; }
    public bool ManageNews { get; set; }
}
public IActionResult Users()
{
     var viewModel = new UsersViewModel();
     return View(viewModel);
}
@model Website.Models.UsersViewModel
<div class="form-check">
    @Html.CheckBoxFor(x => x.IsAdmin)
    <label class="form-check-label" for="defaultCheck1">
           Admin
    </label>
</div>