如何模仿MVC的复选框->布尔模型绑定?

发布于 2024-12-14 03:18:03 字数 694 浏览 1 评论 0原文

我有一个编辑器模板,它呈现一个复选框:

@Html.CheckBoxFor(model => model.Follow)

呈现类似这样的内容:

<input checked="checked" data-val="true" data-val-required="The Follow field is required." id="Follow" name="Follow" type="checkbox" value="true" />
<input name="Follow" type="hidden" value="false" />

AFAIK,当未选中的框未发送到服务器或其他内容时,隐藏字段与餐饮有关。

无论如何,如果我在选中复选框时查看 Request.Form["Follow"],我会看到值“true,false”。

我如何从该值强制转换为 bool ?我是否只是忽略第二个字段? (例如隐藏字段)。

我正在做的是一个基本控制器(受保护的方法,从子控制器调用),所以我没有强类型视图模型,只有原始 Request 对象。

有人可以帮忙吗?或者,如果有人可以指出 MVC 源代码中发生这种情况的位置,我可以自己查看一下,但不确定从哪里开始查找。

I've got an editor template which renders out a checkbox:

@Html.CheckBoxFor(model => model.Follow)

Which renders something like this:

<input checked="checked" data-val="true" data-val-required="The Follow field is required." id="Follow" name="Follow" type="checkbox" value="true" />
<input name="Follow" type="hidden" value="false" />

AFAIK the hidden field is something to do with catering when an unchecked box isn't sent to the server or something.

Anyway, if i take a look at the Request.Form["Follow"] when the checkbox is checked, i see a value of "true,false".

How do i coerce a bool from this value? Do i simply ignore the second field? (e.g the hidden field).

I'm doing this is a base controller (protected method, invoked from child controller), so i don't have a strongly-typed view model, only the raw Request object.

Can anyone help? Or alternatively, if someone could point me to where in the MVC source code this happens, i could take a look myself, but not sure where to start looking.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抽个烟儿 2024-12-21 03:18:03

您是对的,隐藏字段只是将表单提交到服务器。因为如果表单只有未选中的复选框,则不会提交任何内容,并且服务器不知道将它们设置为 false。

  1. 每个表单只需要 1 个隐藏字段,每个复选框不需要一个。但是,如果您制作自己的控件,则很难判断隐藏的文本框是否已经在字段中。如果您知道表单上的其他位置总会有一个文本框或选择列表等,则根本不需要隐藏文本框

  2. 您可以将隐藏文本框重命名为“虚拟”或与复选框名称为 Request.Form["Follow"];只会返回复选框的值,不需要拆分。您永远不需要检查“隐藏文本框”的值。

附带说明一下,您不应该使用 Request.Form["Follow"] 您的 Action 方法应该有一个像这样的参数,而不是“bool? follow”

You are correct the hidden field is just so the form will be submitted to the server. Because if the form had just checkboxes that are not checked then nothing will be submitted and the server would not know to set them to false.

  1. You only require 1 hidden field per form, you do not need one per checkbox. But if your making your own control it is hard to tell if a hidden textbox is already on the field or not. If you know you are always going to have a textbox or select list etc somewhere else on your forms you do not need a hidden textbox at all

  2. You can rename your hidden textbox to anything name it "dummy" or something different to the checkbox name so Request.Form["Follow"]; will only return the value of the check box not need to split. You never need to check the value of the "hidden textbox".

On a side note you shouldn't be using Request.Form["Follow"] you Action method should have a parameter like this instead "bool? follow"

撞了怀 2024-12-21 03:18:03

MVC 帮助器使用两个输入字段(复选框和隐藏字段)呈现复选框输入控件,因为如果未选中复选框,浏览器不会发送复选框输入字段的值。如果您不使用自动映射,则需要解析从表单收到的输入值。

使用这个简单的规则来检测复选框:

var rawFollow = Request.Form["Follow"];
if (rawFollow.Contains("true"))
{
  // do something
}   

MVC helper renders checkbox input control with two input fields, the checkbox and the hidden, because the browser do not send a value for checkbox input field if the checkbox is not selected. If you do not use auto mapping, you need to parse the input value that you receve from your form.

Use this simple rule to detect the checkbox:

var rawFollow = Request.Form["Follow"];
if (rawFollow.Contains("true"))
{
  // do something
}   
挽容 2024-12-21 03:18:03

据我所知,额外的隐藏字段是因为如果未选中该复选框,则该输入将不会随表单一起提交,因此我们需要值为 false 的隐藏字段。

所以唯一能想到的解决方案是这样的:

var rawFollow = Request.Form["Follow"];
var rawFollows = rawFollow.Split(',');
if (rawFollows.Count() > 1)
{
   rawFollow = rawFollows[0];
}

但这看起来很hacky(页面上元素的顺序怎么样,如果由于某种原因隐藏字段是第一个,那么它总是会评估为 false),这就是为什么我想知道 MVC 源代码是如何做到这一点的。

As far as i know, the extra hidden field is because if the checkbox is NOT checked, that input will not be submitted with the form and therefore we need the hidden field with the value of false.

So the only solution is can think of is this:

var rawFollow = Request.Form["Follow"];
var rawFollows = rawFollow.Split(',');
if (rawFollows.Count() > 1)
{
   rawFollow = rawFollows[0];
}

But this seems hacky (and what about the order of the elements on the page, what if for some reason the hidden field was FIRST, then it would always evaluate to false), which is why i'm wondering how the MVC source does this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文