[英]Setting bool in ViewModel using RadioButtonFor?
I have a fairly complex form, but the problem I'm having is with a very basic part of it. 我有一个相当复杂的表格,但是我遇到的问题是其中非常基本的部分。 I have a radio button group with two radio buttons to represent a
true
or false
value for a bool
within my view model. 我有一个带有两个单选按钮的单选按钮组,分别代表视图模型中
bool
的true
或false
值。 So I have a model like this: 所以我有一个像这样的模型:
public class MyViewModel {
public bool IsPOBox { get; set; }
}
Then within my view I have the following code to produce the radio buttons: 然后在我看来,我有以下代码来产生单选按钮:
@Html.RadioButtonFor(m => m.IsPOBox, true, new { id = "togglePOBoxOn" }) @Html.Label("togglePOBoxOn", "PO Box")
@Html.RadioButtonFor(m => m.IsPOBox, false, new { id = "togglePOBoxOff" }) @Html.Label("togglePOBoxOff", "Street Address")
The problem is, when I submit this with "PO Box" selected, the IsPOBox
property is being set to false
instead of true
. 问题是,当我在选择“ PO Box”的情况下提交此文件时,
IsPOBox
属性设置为false
而不是true
。 In fact, the property is set to false regardless of which radio button is selected. 实际上, 无论选择哪个单选按钮,该属性都设置为false。
Have I messed something up here? 我在这里弄乱了吗?
It turns out ASP.NET MVC doesn't include disabled fields' values when you submit a form. 事实证明,提交表单时,ASP.NET MVC不包括禁用字段的值。 The field was being disabled by a checkbox further up in the form.
表单上方的复选框已禁用该字段。 Annoying!
烦!
Edit: After seeing @JohnSaunders' comment below I did a quick search for how I'm supposed to have an uneditable field, but still have the information submitted. 编辑:看到下面的@JohnSaunders评论后,我快速搜索了应该如何拥有不可编辑的字段,但仍然提交了信息。 It turns out you should use the
readonly
attribute. 事实证明,您应该使用
readonly
属性。 I learned something today! 我今天学到了一些东西!
ie <input type="text" readonly />
即
<input type="text" readonly />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.