DropDownListFor(...) 默认选择 boolean false
我在应用程序中有典型的 YesNo 类型的下拉菜单。为此,我开发了模型(而不是 ViewModel Utility)类以供将来扩展。
public string Text { get; set; } // represents text part
public bool Value { get; set; } // represent value
public List<DropDown> DropDowns { get; set; } //list for binding
public void BuildYesNoDropDown()
{
DropDowns = new List<DropDown>();
DropDowns.Add(new DropDown { Text = "Yes", Value = true });
DropDowns.Add(new DropDown { Text = "No", Value = false });
}
然后,我将其绑定在视图中,如下所示:
@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.DropDowns, "Value", "Text",1),"Select") //last para - OptionLabel
在视图上,所有三个参数都会显示,即“选择”、“是”和“否”。但是,默认情况下已选择“否”。如果我将“Value”属性设置为整数,那么它可以正常工作,并且默认情况下“Select”会被选择,但正如代码中提到的,如果我倾向于使用 bool 类型,则“No”会被选择。
当 DataValueField 为 bool 时如何获得正常行为?
I have typical YesNo kind of dropdown in the application. For that, I have developed model (rather ViewModel Utility) class for future extension prupose.
public string Text { get; set; } // represents text part
public bool Value { get; set; } // represent value
public List<DropDown> DropDowns { get; set; } //list for binding
public void BuildYesNoDropDown()
{
DropDowns = new List<DropDown>();
DropDowns.Add(new DropDown { Text = "Yes", Value = true });
DropDowns.Add(new DropDown { Text = "No", Value = false });
}
Then, I bind it in view like following:
@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.DropDowns, "Value", "Text",1),"Select") //last para - OptionLabel
On the view, all three parameters are getting display i.e. "Select", "Yes" and "No". But, by default "No" has been selected. If I make "Value" property as integer then it works fine and by default "Select" gets selected, but as mentioned in the code, if I tend to go with bool type then "No" gets selected.
How to get normal behavior when DataValueField is bool?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ViewModel
视图
ViewModel
View
模型:
视图:
这会自动生成一个下拉列表,其 3 种状态与可为 null 的布尔值的 3 种状态相匹配:null、true、false。如果您需要回答值,您可以使用数据注释,如下所示:
模型:
要点是您的模型需要是一个真实的模型 - 它需要对从未设置到设置的输入值进行建模。如果您需要输入
int
,您可能希望使用可为 null 的 int(因此int?
)并需要该值。这样初始值为 null 而不是 0。它与其他数据类型类似,除了string
已经可以为 null 了。Model:
View:
This makes a drop down list automatically with 3 states that match the 3 states of a nullable bool: null, true, false. If you require that Value be answered you can use data annotations like so:
Model:
The takeaway is your model needs to be a real model -- it needs to model the input values from unset to set. If you need to input a
int
, you'll likely want to use a nullable int (soint?
) and require the value. That way the initial value is null instead of 0. It is similar with other data types exceptstring
which is already nullable.就我而言,我在同一页面上有一堆这样的下拉菜单,我认为我会很聪明并重用选择列表,例如
视图中
在不起作用的 。我为每个下拉菜单初始化了一个单独的选择列表,这解决了问题
In my case I had a bunch of these dropdowns on the same page and I thought I'd be clever and reuse the select list, e.g.
and in the view
that wasn't working. I initialized a separate select list for each dropdown and that fixed the problem
我知道这是一个古老的话题,但我认为对于其他人来说,了解一些背景知识可能会派上用场。
bool 的默认值为 false
默认情况下选择 no,因为如果您只是将模型设置为 a,则
在您的视图模型中像这样可以为 null 的 Bool ,那么它应该默认选择您的默认值。 :
然后你只需在视图中设置下拉列表:
结果应该是,当布尔为空时,自动选择的选项应该是你的默认值
{Default Select Value}
I know this is a old topic but I thought that for others this might come in handy to know a little background about.
no is selected by default because the default value of a bool is false
if you just set the model to be a
null-able Bool like this in your view model then it should by default select your default value. :
and then you just set your dropdownlist like this in the view:
the result of this should be that when the bool is null the automatically selected option should be your default value
{Default Select Value}