2

I have a dropdownlistfor with True/False values. I want the default value to be true. Right now it's false because the default of a bool is false. This dropdown sometimes gets created dynamically in javascript so I can't set the value in the model before it gets passed. How do I set the default to True in the chtml file?

Thank you.

//----------  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

mschu
  • 95
  • 2
  • 6

1 Answers1

5
@Html.DropDownListFor(m => m.IsBoolFilterCondition, 
        new SelectList(
            new[]
            {
                new SelectListItem { Value = "1", Text = "True" ,Selected = true},
                new SelectListItem { Value = "0", Text = "False" },
            },
            "Value",
            "Text"
            ),
            new { @class="valuebool"}
        )
malkam
  • 2,337
  • 1
  • 14
  • 17
  • Thanks Malkam, unfortunately when I add ", Selected = true" to my SelectList, I get this error error CS0826: No best type found for implicitly-typed array – mschu May 13 '14 at 19:54
  • Set Selected in SelectListItem as above – malkam May 14 '14 at 07:05
  • Sorry. My comment was unclear. I did put the ", Selected = true" in the SelectedItem CTOR, just as you show above. When I did this, I get the error "No best type found for implicitly-typed array". @Html.DropDownListFor(m => m.IsBoolFilterCondition, new SelectList( new[] { new { Value = "true", Text = "True", Selected = true }, new { Value = "false", Text = "False" }, }, "Value", "Text" ), new { @class="valuebooldll"} ) – mschu May 14 '14 at 14:18
  • Set Selected=false for second one – malkam May 14 '14 at 15:04
  • I put selected = false and it now compiles and runs, but it still defaults to false. Any other ideas : @Html.DropDownListFor(m => m.IsBoolFilterCondition, new SelectList( new[] { new { Value = "true", Text = "True", Selected = true }, new { Value = "false", Text = "False", Selected = false }, }, "Value", "Text" ), new { @class="valuebooldll"} ) – mschu May 14 '14 at 19:12
  • I still need a way to set the bool default value to true. Anyone? – mschu May 19 '14 at 12:30
  • I hope IsBoolFilterCondition is of type boolean. As per http://stackoverflow.com/questions/18235229/dropdownlistfor-with-an-item-value-0-always-selected,always false is selected as boolen default value is false.To work around make IsBoolFilterCondition nullable boolean type i.e bool? or change SelectListItem values to "1"/"0" instead of "true"/"false". I'm updating my solution with "1"/"0". See if it works for you. – malkam May 19 '14 at 18:19
  • Setting values to 1 and 0 works. Of course I can no longer bind to a bool type, but I can bind to an int and use the int to set a bool value in the controller action method. I am marking your solution as the answer. Thank you very much for your help. But if you know of a better way of handling the data binding to an int vs bool I would like to hear of it. – mschu May 21 '14 at 12:31
  • Try this http://stackoverflow.com/questions/23799091/html-dropdownlistfor-how-to-set-default-value?noredirect=1#comment36607172_23799091 – malkam May 22 '14 at 07:44