MVC2复选框问题 [英] MVC2 Checkbox problem

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

问题描述

当我使用的HTML帮助复选框,它会产生2表单元素。我明白这是为什么,我都没有问题,除非是:

When I used the html Helper Checkbox, it produces 2 form elements. I understand why this is, and I have no problem with it except:

复选框的未检查似乎并没有在与'隐藏'值同步。

The un-checking of the checkbox does not seem to be in sync with the 'hidden' value.

我的意思是,当我有一大堆的复选框在一个循环中产生:

What I mean is that when I have a bunch of checkboxes being generated in a loop:

<%=Html.CheckBox("model.MarketCategories[" & i & "].Value", category.Value)%>

和用户取消选择和复选框和category.Value为FALSE,正在生成code是:

and the user deselects and checkbox and the category.Value is FALSE, the code being generated is:

<input checked="checked" id="model_MarketCategories_0__Value" name="model.MarketCategories[0].Value" type="checkbox" value="true" />
<input name="model.MarketCategories[0].Value" type="hidden" value="false" />

这是错误的,因为值为False复选框不应该被选中。

This is wrong since the Value is False the checkbox should NOT be checked.

任何想法,为什么发生这种情况?

Any ideas why this is happening?

推荐答案

更​​糟糕的是,当它的提出,它显示为真,假。非常令人沮丧的。

What's worse, is when it's submitted, it shows up as "true,false". Very frustrating.

当您选中该复选框编程,它不设置相关的隐藏字段。您可以轻松地编写复选框的标记,而不是直接使用MVC控制解决这个问题。

When you check the box programmatically, it doesn't set the associated hidden field. You can easily work around this by writing the markup for the checkbox directly instead of using the MVC control.

我已经刚刚这个做我自己。这是我的一个忌讳。

I've had to do this myself just recently. It's a pet peeve of mine.

请参阅此链接了解的更多信息这一点。

See this link for more information on this.

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

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