MVC Html.CheckBox 和表单提交问题

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

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

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 和表单提交问题

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

发布评论

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

>www.elefans.com

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