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?
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.
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)
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
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}