MVC Html.CheckBox和表单提交问题

编程入门 行业动态 更新时间:2024-10-28 02:30:55
本文介绍了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 }

$ b b

您检查为false的原因,因为Html复选框助手对值真的做某些怪异的事情

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

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返回为:

it makes the string read "false"

更多推荐

MVC Html.CheckBox和表单提交问题

本文发布于:2023-10-11 13:56:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481894.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   MVC   Html   CheckBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!