Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have this:</p> <pre><code>public static class BoolUtility { public static IEnumerable&lt;SelectListItem&gt; SelectList(string defaultText = null, string defaultTrue = "True", string defaultFalse = "False") { var list = new List&lt;SelectListItem&gt; { new SelectListItem {Text = defaultTrue, Value = "True"}, new SelectListItem {Text = defaultFalse, Value = "False"} }; if (defaultText != null) { list.Insert(0, new SelectListItem { Text = defaultText, Value = string.Empty }); } return list; } } </code></pre> <p>And use it like this:</p> <pre><code>@Html.DropDownListFor(m =&gt; m.SomeBoolProperty, BoolUtility.SelectList("All", "Yes", "No")) </code></pre> <p>And it seems to work well. Pretty flexible, since you can control all of the labels, and whether or not there is a 'default' value. </p> <p>Oh, and some NUnit unit tests, if you like. By no means comprehensive, but this isn't that complicated...</p> <pre><code>[TestFixture] class BoolUtilityTests { [Test] public void Parameterless() { var actual = BoolUtility.SelectList().ToList(); Assert.That(actual.Count, Is.EqualTo(2)); Assert.That(actual.First().Text, Is.EqualTo("True")); Assert.That(actual.First().Value, Is.EqualTo("True")); Assert.That(actual.Last().Text, Is.EqualTo("False")); Assert.That(actual.Last().Value, Is.EqualTo("False")); } [Test] public void LabelOverrides() { var actual = BoolUtility.SelectList(defaultTrue: "Yes", defaultFalse: "No").ToList(); Assert.That(actual.Count, Is.EqualTo(2)); Assert.That(actual.First().Text, Is.EqualTo("Yes")); Assert.That(actual.First().Value, Is.EqualTo("True")); Assert.That(actual.Last().Text, Is.EqualTo("No")); Assert.That(actual.Last().Value, Is.EqualTo("False")); } [Test] public void IncludeDefaultOption() { var actual = BoolUtility.SelectList(defaultText: "all").ToList(); Assert.That(actual.Count, Is.EqualTo(3)); Assert.That(actual.First().Text, Is.EqualTo("all")); Assert.That(actual.First().Value, Is.EqualTo(string.Empty)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload