MVC Html.CheckBox 和表单提交问题 [英] MVC Html.CheckBox and form submit issue

查看:21
本文介绍了MVC Html.CheckBox 和表单提交问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ASP.NET MVC RC 中提交 Html.Checkbox 值的疯狂问题

Crazy issue with submitting of values in Html.Checkbox in ASP.NET MVC RC

某些值只是没有出现在 Request.Params 中

Some of the values are just not come to Request.Params

在我的表单中,循环中有这一行:

<%=Html.CheckBox("cb" + p.Option.Id, p.Option.IsAllowed, new { value = 6 })%>

At my form I have this line inside the cycle:

<%=Html.CheckBox("cb" + p.Option.Id, p.Option.IsAllowed, new { value = 6 })%>

然后渲染到下一个:

    <input checked="checked" id="cb17" name="cb17" type="checkbox" value="6" />
<input name="cb17" type="hidden" value="false" /> 

    <input checked="checked" id="cb18" name="cb18" type="checkbox" value="6" />
<input name="cb18" type="hidden" value="false" /> 

    <input id="cb19" name="cb19" type="checkbox" value="6" />
<input name="cb19" type="hidden" value="false" />

    <input id="cb20" name="cb20" type="checkbox" value="6" />
<input name="cb20" type="hidden" value="false" />

    <input checked="checked" id="cb21" name="cb21" type="checkbox" value="6" />
<input name="cb21" type="hidden" value="false" /> 

提交表单后,我得到如下信息:

After submitting the Form I'm get something like:

Form.Params["cb17"] = {6, "false"}
Form.Params["cb18"] = {6, "false"}
Form.Params["cb19"] = {"false"}
Form.Params["cb20"] = {"6,false"}
Form.Params["cb21"] = {"false"}

在请求字符串中,有些参数显示两次(正常情况),有些只显示一次(仅隐藏字段的值).似乎它不依赖于是否选中了复选框,值是否发生了变化等等......

In the request string Some of the params are displayed twice (normal situation) and some only ONE TIME (only value of hidden field). It seems that it doesn't rely on whether checkbox was checked or not, whether value has changed or so...

有人遇到过这种情况吗?我该如何解决?

Does anybody faced with such a situation? How can I work around?

推荐答案

   <% using(Html.BeginForm("Retrieve", "Home")) %>//Retrieve is the name of the action while Home is the name of the controller
       <% { %>
    <%foreach (var app in newApps)              { %>  
  <tr> 
       <td><%=Html.CheckBox(""+app.ApplicationId )%></td>      

   </tr>  
<%} %>
 <input type"submit"/>
<% } %>

并在您的控制器中

 List<app>=newApps; //Database bind
 for(int i=0; i<app.Count;i++)
 {

    var checkbox=Request.Form[""+app[i].ApplicationId];
    if(checkbox!="false")// if not false then true,false is returned
 }

您检查 false 的原因是因为 Html Checkbox 助手为 true 值做了一些奇怪的事情

the reason you check for false because the Html Checkbox helper does some kind of freaky thing for value true

True 返回为:

it makes the string read "true, false"

所以你可能认为它是两个值,但它只是一个并且意味着真实

so you may have thought it was two values but its just one and means true

假返回为:

it makes the string read "false"

这篇关于MVC Html.CheckBox 和表单提交问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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