CheckBox和表单提交问题

CheckBox和表单提交问题

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

问题描述

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

某些值只是不是来Request.Params

在我的形式我有周期内这一行:搜索结果
<%= Html.CheckBox(CB+ p.Option.Id,p.Option.IsAllowed,新{值= 6})%GT;

和它呈现到下一:

 <输入检查=选中ID =CB17NAME =CB17类型=复选框VALUE =6/>
<输入名称=CB17类型=隐藏值=FALSE/>    <输入检查=选中ID =CB18NAME =CB18类型=复选框VALUE =6/>
<输入名称=CB18类型=隐藏值=FALSE/>    <输入ID =cb19NAME =cb19类型=复选框VALUE =6/>
<输入名称=cb19类型=隐藏值=FALSE/>    <输入ID =CB20NAME =CB20类型=复选框VALUE =6/>
<输入名称=CB20类型=隐藏值=FALSE/>    <输入检查=选中ID =CB21NAME =CB21类型=复选框VALUE =6/>
<输入名称=CB21类型=隐藏值=FALSE/>

在提交表格我后得到的东西,如:

  Form.Params [CB17] = {6,假}
Form.Params [CB18] = {6,假}
Form.Params [cb19] = {假}
Form.Params [CB20] = {6,假}
Form.Params [CB21] = {假}

在请求字符串有些PARAMS都显示两次(正常情况下),以及一些只有一次(只能隐藏字段的值)。
看来,它不依赖于复选框是否被选中与否,价值是否有变化等等...

有没有人遇到这样的情况?我该如何解决?


解决方案

 <使用%(Html.BeginForm(检索,家))%GT; //检索是操作的名称,同时,首页是控制器的名
       <%{%GT;
    <%的foreach(在newApps VAR应用){%GT;
  &所述; TR>
       &所述; TD>&下;%= Html.CheckBox(+ app.ApplicationId)%GT;&下; / TD>   < / TR>
<%}%GT;
 <输入类型提交/>
<%}%GT;

和在你的控制器

 列表<应用程式并GT = newApps; //数据库绑定
 的for(int i = 0; I< app.Count;我++)
 {    VAR复选框=的Request.Form [+应用[I] .ApplicationId]
    如果(复选框!=假)//如果不是假的,则为true,返回false
 }

您检查虚假因为HTML复选框助手完成某种奇特的事情true值的原因。

真正的收益为:

 它使读取字符串真,假

所以你可能会以为这是两个值,但其只是一个手段和真正的

假回报为:

 它使读取字符串假

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

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

At my form I have this line inside the cycle:

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

and it renders to next:

    <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"/>
<% } %>

and in your controller

 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
 }

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

True returns as:

it makes the string read "true, false"

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

False returns as:

it makes the string read "false"

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

08-19 15:23