提交后的mvc3复选框值 [英] mvc3 checkbox value after submit

查看:202
本文介绍了提交后的mvc3复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含2个字段的表单,一个下拉列表和一个复选框。我有一切正常工作,但我不能为某些原因获得复选框的值,如果它被选中这是我的代码..

  [HttpPost] 
public ActionResult视图(字符串选择)
{

切换(选择)
{
caseDeny:
//捕获复选框值
break;
caseAccept:
//捕获此处的复选框值
break;

}
return View();
}

这是我的观点

  @using(Html.BeginForm(view,grouprequest,FormMethod.Post,new {}))
{


@ Html.DropDownList(pick,new SelectList(new List< Object> {
new {Text =Accept,Value =Accept},new {Text =Deny deny}},Value,Text),new {})

< input type =submitname =workid = style =font-size:16px/>

foreach(模型中的var项)
{
< input type =checkboxid =@ item.grouprequestIDname =@ item.grouprequestIDvalue = @ item.grouprequestID/>


}
}

基本上,具有接受拒绝的两个选项,我可以通过控制器中的 SWITCH-case 捕获用户选择的那个,现在如何捕获复选框的值?如果你注意到复选框有一个名为 @groupRequestID 的变量,所以每个复选框都有不同的唯一值,如1,2,3等。任何帮助将非常感激!



模型

  public class grouprequest 
{
[Key ]
public int grouprequestID {get;组; }
public int? profileID {get;组; }
public int? registrationID {get;组; }
public DateTime expires {get;组; }
public int? Grouplink {get;组; }
}


解决方案

服务器行为有点奇怪。
如果选中框,浏览器将发送 name = value ,如

 < input type =checkboxname =namevalue =value/> 

但如果复选框是,则检查服务器不发送任何内容。

 < input type =checkboxname =Check1id =Checks1value =Hellochecked = checked/> 
< input type =checkboxname =Check1id =Checks1value =Hello1/>
< input type =checkboxname =Check1id =Checks1value =Hello2/>

会导致 Check1 = Hello p>

这意味着如果所有的复选框都是相关的,命名它们将会填充与ActionMethod相同的属性。



如果你在你的视图中有这个:

 < input type =checkboxname =MyValuesvalue =1checked =checked/& 
< input type =checkboxname =MyValuesvalue =2/>
< input type =checkboxname =MyValuesvalue =3/>

这是您的控制器操作方法:

  public ActionMethod MyAction(IEumerable< int> myValues)

myValues 变量将如下所示:

  myValues [0] = = 1 

您还应注意,如果您使用 Html 辅助扩展:

  @ Html.CheckBoxFor(m => m.MyValue)

其中 MyValue bool 该扩展将创建一个复选框输入标签以及一个具有相同名称的 hidden 输入标签,这意味着一个值将被传递到控制器方法中。



希望这有助于。


I have a form with 2 fields a dropdownlist and a checkbox. I have everything working correctly but i can not for some reason obtain the value of a checkbox if it is checked this is my code..

[HttpPost]
    public ActionResult view(string pick)
    {

        switch (pick)
        {
            case "Deny":
                // capture checkbox value here
                break;
            case "Accept":
                // capture checkbox value here
                break;

        }
        return View();
    }

This is my view

@using (Html.BeginForm("view", "grouprequest", FormMethod.Post, new {}))
{


 @Html.DropDownList("pick", new SelectList(new List<Object>{ 
                new{ Text ="Accept", Value= "Accept"},new{ Text ="Deny", Value= "Deny"}},    "Value", "Text"), new {})

 <input type="submit" name="work" id="work" value="Update" style="font-size:16px" />

foreach (var item in Model)
{
 <input type="checkbox" id="@item.grouprequestID" name="@item.grouprequestID"  value="@item.grouprequestID" />


}
}

Basically the dropdownlist has 2 options which are Accept and Deny I can capture which one the user chooses via the SWITCH-case in the controller now how can I capture the value of the checkboxes? If you notice the Checkboxes have a variable to them named @groupRequestID so every checkbox has a different unique value like 1,2,3 etc.. any help would be greatly appreciated !!

The Model

 public class grouprequest
{
    [Key]
    public int grouprequestID { get; set; }
    public int? profileID { get; set; }
    public int? registrationID { get; set; }
    public DateTime expires { get; set; }
    public int? Grouplink { get; set; }
}

解决方案

Check boxes when posted to the server act a little strange. If a box is checked the browser will send name=value as in

<input type="checkbox" name="name" value="value" />

But if the checkbox is not checked the server doesn't send anything.

<input type="checkbox" name="Check1" id="Checks1" value="Hello" checked="checked"/>
<input type="checkbox" name="Check1" id="Checks1" value="Hello1" />
<input type="checkbox" name="Check1" id="Checks1" value="Hello2" />

Will result in Check1 = Hello

What this means is if all your check boxes are related, naming them the same will populate the same attribute of your ActionMethod. If that attribute is an enumeration it will contain only the ones that are checked.

If you have this in your view:

<input type="checkbox" name="MyValues" value="1" checked="checked"/>
<input type="checkbox" name="MyValues" value="2" />
<input type="checkbox" name="MyValues" value="3" />

and this as your controller action method:

public ActionMethod MyAction(IEumerable<int> myValues)

The myValues variable will look like this:

myValues[0] == 1

You should also note that if you are using the Html helper extension:

@Html.CheckBoxFor(m => m.MyValue)

Where MyValue is a bool the extension will create a checkbox input tag and also a hidden input tag with the same name, meaning a value will always be passed into the controller method.

Hope this helps.

这篇关于提交后的mvc3复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆