[英]Why Html.Checkbox(“Visible”) returns “true, false” in ASP.NET MVC 2?
I'm using Html.Checkbox("Visible")
for displaying a check box to user. 我正在使用
Html.Checkbox("Visible")
向用户显示一个复选框。 In post back, FormCollection["Visible"]
value is "true, false". 在回发中,
FormCollection["Visible"]
值为“true,false”。 Why? 为什么?
in view: 在视图中:
<td>
<%: Html.CheckBox("Visible") %>
</td>
in controller: 在控制器中:
adslService.Visible = bool.Parse(collection["Visible"]);
That's because the CheckBox
helper generates an additional hidden field with the same name as the checkbox (you can see it by browsing the generated source code): 这是因为
CheckBox
帮助器生成了一个与复选框同名的附加隐藏字段(您可以通过浏览生成的源代码来查看它):
<input checked="checked" id="Visible" name="Visible" type="checkbox" value="true" />
<input name="Visible" type="hidden" value="false" />
So both values are sent to the controller action when you submit the form. 因此,当您提交表单时,这两个值都将发送到控制器操作。 Here's a comment directly from the ASP.NET MVC source code explaining the reasoning behind this additional hidden field:
这是一个直接来自ASP.NET MVC源代码的注释,解释了这个额外隐藏字段背后的原因:
if (inputType == InputType.CheckBox) {
// Render an additional <input type="hidden".../> for checkboxes. This
// addresses scenarios where unchecked checkboxes are not sent in the request.
// Sending a hidden input makes it possible to know that the checkbox was present
// on the page when the request was submitted.
...
Instead of using FormCollection
I would recommend you using view models as action parameters or directly scalar types and leave the hassle of parsing to the default model binder: 我建议您使用视图模型作为操作参数或直接标量类型,而不是使用
FormCollection
,并将解析的麻烦留给默认的模型绑定器:
public ActionResult SomeAction(bool visible)
{
...
}
I've recently dealt with this issue and came up with a method of bypassing the MVC binding and using Contains("true") on the query string. 我最近处理了这个问题,并提出了一种绕过MVC绑定并在查询字符串上使用Contains(“true”)的方法。 Nothing else worked for me.
没有其他任何对我有用。
If people are stuck with the other answers then this is what worked for me - http://websitesorcery.com/post/2012/03/19/CheckBox-Issue-with-MVC-3-WebGrid-Paging-Sorting.aspx 如果人们坚持其他答案,那么这对我有用 - http://websitesorcery.com/post/2012/03/19/CheckBox-Issue-with-MVC-3-WebGrid-Paging-Sorting.aspx
If you want/need to use FormCollection
, instead of checking for true
or false
, check for true,false
or false
. 如果您希望/需要使用
FormCollection
,而不是检查true
或false
,请检查true,false
或false
。
eg instead of this 例如,而不是这个
adslService.Visible = bool.Parse(collection["Visible"]);
do this 做这个
adslService.Visible = bool.Parse(collection["Visible"] != "false");
I had the same issue in MVC 4, 我在MVC 4中遇到了同样的问题,
This solution worked for me. 这个解决方案对我有用。
My (form)View: 我的(表格)查看:
@Html.EditorFor(m => m.SomeBoolValue)
For voiding the duplicate "true,false" values when CheckBox is true 用于在CheckBox为true时清空重复的“true,false”值
I stored the CheckBox param in an array: 我将CheckBox参数存储在一个数组中:
var arrParams = Request.Form["SomeBoolValue"].Split(',');
Grab the first element: 抓住第一个元素:
string sParam = arrParams[0].ToString();
Parse it: 解析它:
bool BoolValue = bool.Parse(sParam);
Keep it: 收下:
MyObject.SomeBoolValue = BoolValue;
I had the same issue. 我遇到过同样的问题。 I fixed it with the following combination (with a little help from Darin Dimitrov--thank you):
我用以下组合修复它(在Darin Dimitrov的帮助下 - 谢谢):
VIEW: 视图:
<label for="optIn"><%=Html.CheckBox("optIn", ViewData["OptIn"])%>
Controller: 控制器:
public ActionResult Index()
{
ViewData["Optin"] = True;
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection form, bool OptIn )
{
ViewData["Optin"] = OptIn;
}
Here's the source for the control with and without the checkbox actually checked (for reference): 这里是控件的来源,有和没有实际选中的复选框(供参考):
Checked: 经过:
<input Length="4" checked="checked" id="optIn" name="optIn" type="checkbox" value="true" /><input name="optIn" type="hidden" value="false" />
Unchecked: 未选中:
<input Length="4" id="optIn" name="optIn" type="checkbox" value="true" /><input name="optIn" type="hidden" value="false" />
So, here's how I am interpreting the behavior: 所以,这就是我解释行为的方式:
HTML will not post back the field value if the checkbox is unchecked but will post back if it is. 如果取消选中该复选框,HTML将不会回发字段值,但如果是,则会回发。 The helper appends a hidden field after the checkbox control (value of 'False').
帮助器在复选框控件之后附加隐藏字段(值为'False')。 If the checkbox is checked, the source shows "checked = 'checked'" if it's unchecked, this does not appear.
如果选中该复选框,则源显示“checked ='checked'”如果未选中,则不显示。 So, if checked = checked, true is passed back to the controller.
因此,如果checked = checked,则将true传递回控制器。 If the box is unchecked, the control is not passed back so the hidden field, named the same, takes over and passes back a false.
如果未选中此框,则不会传回控件,因此名为相同的隐藏字段将接管并传回false。 This way you have both conditions.
这样你就有两个条件。 Strange but it works.
奇怪,但它的工作原理。 I hope this helps.
我希望这有帮助。
The default behaviour of the MVC Entension for Html.Checkbox, generates a hidden field along with it to be posted. MVC Entension for Html.Checkbox的默认行为会生成一个隐藏字段以及要发布的字段。 In my case, Because the custom checkbox I have added to page is not model binded like Html.CheckBoxFor, I have decided to make it as simple html checkbox.
在我的情况下,因为我添加到页面的自定义复选框不像Html.CheckBoxFor那样绑定模型,所以我决定将其设置为简单的html复选框。
<input type="checkbox" name="WithAttachment" checked="checked"/> Include Attachments
This may sound silly, but would save someone's time than running after work arounds. 这可能听起来很愚蠢,但可以节省一些人的时间,而不是在工作后跑步。 This will prevent from creating a checkbox using MVC extension
这将阻止使用MVC扩展创建复选框
I already have a static function that I use everywhere to handle boolean type text and return true or false as a boolean so I just take care of it there 我已经有一个静态函数,我在任何地方使用它来处理布尔类型文本并返回true或false作为布尔值所以我只是在那里处理它
if (sBool.StartsWith("true,")) bReturn = true;//cater for mvc checkboxes
else if (sBool == "true") bReturn = true;
else if (sBool == "t") bReturn = true;//t-f
else if (sBool == "1") bReturn = true;//1-0
else if (sBool == "yes") bReturn = true;//yes-no
else if (sBool == "y") bReturn = true;//y-n
else if (sBool == "on") bReturn = true;//on-off
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.