Bespoke Software Solutions in C#.Net, ASP.Net MVC, JQuery, MEF and more
Yes/No Dropdownlist Extension for ASP.NET MVC
I am not a big fan of using checkboxes for a simple yes/no question on a form. I use checkboxes only for selecting multiple items to perform an action on, or when I am using a checkboxlist. The reason for this is because I believe a yes/no question has three possible states, not only two.
If you are asking a user “Do you agree with our terms?”, the general consensus is to assume that not clicking the box is “forgetting” to make a selection and warning me that “You have forgotten to tick our terms and conditions”. In this situation the case may be so, but what if I did not agree? What if the question was “Do you want to opt out of us sending you loads and loads of spam?” and simply not spotting this question.
I believe that if you did not make a selection it is a valid state. Thus a yes/no question, in my opinion, has the following states; “yes”, “no” and “did not choose”.
Once again, I don’t simply rant, I also offer a solution… a very simple extension method which I love using for all my yes/no questions instead of checkboxes. In this case I made the “not selected” state 0.
public static string YesNoDropDownList(this HtmlHelper helper, string id, string selectedValue)
{
var list = new SelectList(new[]
{
new {text = "Select", value = "0"},
new {text = "No", value = "1"},
new {text = "Yes", value = "2"}
}, "value", "text", selectedValue);
return helper.DropDownList(id, list);
}
PS: It also looks a bit neater and more uniform.