DropDownListFor(...) 默认选择 boolean false

发布于 2024-12-08 16:00:11 字数 921 浏览 0 评论 0原文

我在应用程序中有典型的 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 技术交流群。

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

发布评论

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

评论(4

嘿嘿嘿 2024-12-15 16:00:11

ViewModel

public bool flag { get; set; }

视图

@Html.DropDownListFor(model => model.flag, new List<SelectListItem>()
{
   new SelectListItem() { Text = "Yes", Value = "True" },
   new SelectListItem() { Text = "No", Value = "False"}
}, "Select.....", new { @id = "flag", @class="form-control" })
@Html.ValidationMessageFor(model => model.flag)

ViewModel

public bool flag { get; set; }

View

@Html.DropDownListFor(model => model.flag, new List<SelectListItem>()
{
   new SelectListItem() { Text = "Yes", Value = "True" },
   new SelectListItem() { Text = "No", Value = "False"}
}, "Select.....", new { @id = "flag", @class="form-control" })
@Html.ValidationMessageFor(model => model.flag)
呆° 2024-12-15 16:00:11

模型:

public bool? Value { get; set; }

视图:

@Html.EditorFor(model => model.Value)

这会自动生成一个下拉列表,其 3 种状态与可为 null 的布尔值的 3 种状态相匹配:null、true、false。如果您需要回答值,您可以使用数据注释,如下所示:

模型:

[Required]
public bool? Value { get; set; }

要点是您的模型需要是一个真实的模型 - 它需要对从未设置到设置的输入值进行建模。如果您需要输入 int,您可能希望使用可为 null 的 int(因此 int?)并需要该值。这样初始值为 null 而不是 0。它与其他数据类型类似,除了 string 已经可以为 null 了。

Model:

public bool? Value { get; set; }

View:

@Html.EditorFor(model => model.Value)

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:

[Required]
public bool? Value { get; set; }

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 (so int?) and require the value. That way the initial value is null instead of 0. It is similar with other data types except string which is already nullable.

说好的呢 2024-12-15 16:00:11

就我而言,我在同一页面上有一堆这样的下拉菜单,我认为我会很聪明并重用选择列表,例如

var YesOrNoTriState = new List<SelectListItem> {
  new SelectListItem { Text = "Select", Value = "" },
  new SelectListItem { Text = "Yes", Value = true.ToString() },
  new SelectListItem { Text = "No", Value = false.ToString() }
};

视图中

<%: Html.DropDownListFor(x => x.Field1, Model.YesOrNoTriState) %>
<%: Html.DropDownListFor(x => x.Field2, Model.YesOrNoTriState) %>

在不起作用的 。我为每个下拉菜单初始化了一个单独的选择列表,这解决了问题

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.

var YesOrNoTriState = new List<SelectListItem> {
  new SelectListItem { Text = "Select", Value = "" },
  new SelectListItem { Text = "Yes", Value = true.ToString() },
  new SelectListItem { Text = "No", Value = false.ToString() }
};

and in the view

<%: Html.DropDownListFor(x => x.Field1, Model.YesOrNoTriState) %>
<%: Html.DropDownListFor(x => x.Field2, Model.YesOrNoTriState) %>

that wasn't working. I initialized a separate select list for each dropdown and that fixed the problem

是你 2024-12-15 16:00:11

我知道这是一个古老的话题,但我认为对于其他人来说,了解一些背景知识可能会派上用场。

bool 的默认值为 false

默认情况下选择 no,因为如果您只是将模型设置为 a,则
在您的视图模型中像这样可以为 null 的 Bool ,那么它应该默认选择您的默认值。 :

public bool? Value { get; set; }

然后你只需在视图中设置下拉列表:

@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.DropDowns, "Value", "Text",1),"{Default Select Value}")

结果应该是,当布尔为空时,自动选择的选项应该是你的默认值{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. :

public bool? Value { get; set; }

and then you just set your dropdownlist like this in the view:

@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.DropDowns, "Value", "Text",1),"{Default Select Value}")

the result of this should be that when the bool is null the automatically selected option should be your default value {Default Select Value}

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