MVC DropDownListFor default-Wert für bool

Ich habe eine dropdownlistfor mit True/False-Werte. Ich möchte den default-Wert, um wahr zu sein. Gerade jetzt ist es falsch, weil die Vorgabe einer bool ist false. Diese dropdown-manchmal wird es erstellt dynamisch in javascript, also kann ich nicht den Wert in das Modell, bevor es übergeben bekommt. Wie setze ich den default auf True in der chtml-Datei?

Danke.

//----------  cshtml file ---------------
...
   <td class="valuebool">
        @Html.DropDownListFor(m => m.IsBoolFilterCondition, 
        new SelectList(
            new[]
            {
                //The following two lines defaulted to false
                //new { Value = "true", Text = "True" },
                //new { Value = "false", Text = "False" },

                //The next two lines also defaulted to false
                //new SelectListItem { Value = "true", Text = "True", Selected = true },
                //new SelectListItem { Value = "false", Text = "False", Selected = false },

                //The following two lines also default to false
                new { Value = "true", Text = "True", Selected = true },
                new { Value = "false", Text = "False", Selected = false },                    
            },
            "Value",
            "Text"
            ),
            new { @class="valuebool"}
        )
...

//------- Model Class ------------------------
public class FilterCondition
{
      ...
      public bool IsBoolFilterCondition { get; set; }
      ...
}

//--------------- Browser Source -----------------------

True
False

InformationsquelleAutor mschu | 2014-05-12
Schreibe einen Kommentar