DropDownListFor (...) select boolean false by default

I have a typical YesNo view in the application drop-down list. To do this, I developed a model (rather ViewModel Utility) for a future extension.

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 as shown below:

 @Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.DropDowns, "Value", "Text",1),"Select") //last para - OptionLabel 

In the view, all three parameters are displayed, that is, "Select," "Yes," and "No." But the default is None. If I create the β€œValue” property as an integer, then it works fine, and β€œSelect” is selected by default, but, as indicated in the code, if I tend to go with the bool type, then β€œNo” is selected.

How to get normal behavior when DataValueField is bool?

+6
source share
4 answers

Model:

 public bool? Value { get; set; } 

View:

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

This is an automatically drop-down list with three states that correspond to 3 states of nullable bool: null, true, false. If you need a value to be obtained, you can use data annotations like this:

Model:

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

Stakeout - your model must be a real model - it needs to model input values ​​from unset to set. If you need to enter int , most likely you will want to use nullable int (so int? ) And require a value. Thus, the initial value is zero, not 0. It is similar to other data types except string , which is already nullified.

+7
source

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) 
+9
source

In my case, I had a bunch of these dropdowns on the same page, and I thought I would be smart and reuse the select list, for example.

 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 view

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

which did not work. I initialized a separate selection list for each drop-down list and fixed the problem

+3
source

I know this is an old topic, but I thought it might come in handy for others to learn a little about it.

no is selected by default since the default value for bool is false

if you just set the model as a null-able Bool, as it is in your view model, then it should select your default value by default.

 public bool? Value { get; set; } 

and then you just set your dropdown 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 bool is null, the automatically selected parameter should be your default value {Default Select Value}

+1
source

Source: https://habr.com/ru/post/898778/


All Articles