问题描述
希望对 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...)
第二,谁能解释为什么复选框会这样?假设已勾选的禁用复选框应该 == 真,我是否不正确?禁用状态在语义上是否意味着错误?
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?
推荐答案
使用 disabled="disabled"
属性标记的输入字段NEVER 将其值发送到服务器.如果您想实现这一点,您可以使用 readonly="readonly"
属性,该属性仍可防止用户修改值,但在提交表单时发送现有值.
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.
这篇关于如果禁用,Html.CheckBox 返回 false,即使已选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!