57
@Html.DropDownListFor(model => model.Status, new List<SelectListItem> 
       { new SelectListItem{Text="Active", Value="True"},
         new SelectListItem{Text="Deactive", Value="False"}})

In view I am using this drop dowenlist coding. I run my application default deactive value is display in dropdown list box. I want display default Active

Mary
  • 197
  • 1
  • 15
satheeshkumar
  • 755
  • 2
  • 6
  • 10
  • I hope Status is of type boolean.By default ddl value set to "Deactivate" as boolean default value is "false". Either make Status optional or convert it type to int and set values as 1/0 instead of true/false – malkam May 22 '14 at 06:20
  • http://stackoverflow.com/questions/23610941/mvc-dropdownlistfor-default-value-for-bool/ – malkam May 22 '14 at 06:23
  • @Html.DropDownListFor(model => model.Status, new List { new SelectListItem{Text="Active", Value="1",Selected =true}, new SelectListItem{Text="Deactive", Value="0"}}) It is working correct – satheeshkumar May 22 '14 at 06:38
  • This code only pass the value false only – satheeshkumar May 22 '14 at 08:33
  • Eshan's solution working fine right? – malkam May 22 '14 at 08:52

4 Answers4

78

Like this:

@Html.DropDownListFor(model => model.Status, new List<SelectListItem> 
       { new SelectListItem{Text="Active", Value="True"},
         new SelectListItem{Text="Deactive", Value="False"}},"Select One")

If you want Active to be selected by default then use Selected property of SelectListItem:

@Html.DropDownListFor(model => model.Status, new List<SelectListItem> 
           { new SelectListItem{Text="Active", Value="True",Selected=true},
             new SelectListItem{Text="Deactive", Value="False"}},"Select One")

If using SelectList, then you have to use this overload and specify SelectListItem Value property which you want to set selected:

@Html.DropDownListFor(model => model.title, 
                     new SelectList(new List<SelectListItem>
  {
      new SelectListItem { Text = "Active" , Value = "True"},
      new SelectListItem { Text = "InActive", Value = "False" }
  },
    "Value", // property to be set as Value of dropdown item
    "Text",  // property to be used as text of dropdown item
    "True"), // value that should be set selected of dropdown
     new { @class = "form-control" })
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • I think it' wont work. http://stackoverflow.com/questions/23610941/mvc-dropdownlistfor-default-value-for-bool/ – malkam May 22 '14 at 06:22
  • I tested it on my system first then posted – Ehsan Sajjad May 22 '14 at 06:36
  • @malkam I used 0 and 1 instead of true/false and it worked but whenever I submit the form, the value sent is **False** even if I try to send true/1. Help me out – satheeshkumar May 22 '14 at 07:18
  • @EhsanSajjad: ok, actually I've overlooked. I've used SelectList,but you're using List. – malkam May 22 '14 at 07:47
  • yes you are right i also just noticed – Ehsan Sajjad May 22 '14 at 07:49
  • Its the value of the `Status` property that determines what is selected. Internally the method builds its own `IEnumerable` and sets the `Selected` property based on the value of the property. Setting `Selected=true` in the `SelectListItem` or `SelectList` constructor is ignored when binding to a mdoel. –  Aug 03 '17 at 07:04
  • if the model property contains no value, what about that case? – Ehsan Sajjad Aug 03 '17 at 12:17
  • @EhsanSajjad. Then the first option will be selected (because something has to be). And ditto if the value of the property does not match one of the option values. –  Aug 03 '17 at 23:39
  • None of this worked for me. I had to make sure my equivalent of model.title was set to true by default, since that's what the selection is bound to. – Mir Jun 06 '18 at 16:54
  • Thanks! This worked for me perfectly. – Jagdish Chopde Jan 29 '19 at 15:08
  • what about asp-items and a value when list is empty – Mohamad Elnaqeeb Apr 15 '22 at 08:37
5

SelectListItem has a Selected property. If you are creating the SelectListItems dynamically, you can just set the one you want as Selected = true and it will then be the default.

SelectListItem defaultItem = new SelectListItem()
{
   Value = 1,
   Text = "Default Item",
   Selected = true
};
Krizzy
  • 77
  • 1
  • 3
  • 6
Elad Lachmi
  • 10,406
  • 13
  • 71
  • 133
4
 @Html.DropDownListFor(model => model.UserName, new List<SelectListItem> 
   { new SelectListItem{Text="Active", Value="True",Selected =true },
     new SelectListItem{Text="Deactive", Value="False"}})
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73
1
 @Html.DropDownListFor(model => model.Items, new List<SelectListItem>
   { new SelectListItem{Text="Deactive", Value="False"},
     new SelectListItem{Text="Active", Value="True",  Selected = true},
     })
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Nikunj B. Balar
  • 298
  • 3
  • 21