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")
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?
source share