如果禁用,即使seleced Html.CheckBox返回false [英] Html.CheckBox returns false if disabled, even if seleced
问题描述
但愿你一个简单的问题ASP.NET MVC大师:
Hopefully an easy question for you asp.net mvc gurus:
我有一个复选框,这样创建:
I have a checkbox, created like this:
<%=Html.CheckBox("MyCheckBox", true, new { disabled = "disabled"})%>
在我的行动我检查,像这样的值:
In my action I am checking the value like so:
bool isChecked = form["MyCheckBox"].Contains("true");
我希望这个返回true,因为它被选中。然而时生成隐藏的元素有一个错误的值:
I expect this to return true, as it is checked. However the hidden element that gets created has a false value:
<input checked="checked" disabled="disabled" id="MyCheckBox" name="MyCheckBox" type="checkbox" value="true" />
<input name="MyCheckBox" type="hidden" value="false" />
首先,有没有办法让的HtmlHelper表现得像我希望它应注意什么?或者是手动构建输入/创造我自己的辅助方法的唯一途径? (不,这是一个大问题...)
First, is there a way to make the HtmlHelper behave as I expect it should? Or is manually constructing the input/creating my own helper method the only way? (not that this is a big deal...)
二,任何人都可以解释为什么复选框这样的行为,一些轻?我是不正确的假设,被选中应== true的残疾复选框?是否禁用状态语义的意思是假的?
Second, can anyone shed some light on why the checkboxes behave this way? Am I incorrect in assuming a disabled checkbox that is ticked should == true? Does a disabled state semantically mean false?
推荐答案
标记有禁用=禁用
属性的输入字段的从不其值发送给服务器。如果要实现这一点,你可以使用只读=只读
仍$ P $修改该值pvents的用户,但将现有的数值属性时,形式是提交。
An input field marked with the disabled="disabled"
attribute NEVER sends its value to the server. If you want to achieve this you could use the readonly="readonly"
attribute which still prevents the user from modifying the value but sends the existing value when the form is submitted.
这篇关于如果禁用,即使seleced Html.CheckBox返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!