41

I am using below code to create a drop down

Controller

ViewBag.Id= new SelectList(db.TableName.OrderBy(x => x.Name),"Id","Name")

View

 @Html.DropDownList("Id", null, htmlAttributes: new { @class = "form-control" })

My question is how can I Modify SelectList to add a blank item so that there is a blank item automatically added for DropDownList.

Thanks.

ary
  • 939
  • 2
  • 13
  • 32

4 Answers4

72

Use one of the overloads that accepts an optionLabel

@Html.DropDownListFor(m => m.ID, (SelectList)ViewBag.MyList, "Please select", new { @class = "form-control" })

or if you do not want to use the strongly typed methods

@Html.DropDownList("ID", (SelectList)ViewBag.MyList, "Please select", new { @class = "form-control" })

Which will add the first option with the text specified in the 3rd parameter and with a null value

<option value="">Please Select</option>
  • Thanks for reply. So I cannot modify the SelectList to get the desired result? – ary Jan 29 '16 at 21:05
  • No, but why would you want to? You could always create your own `IEnumerable` and add insert the first one with `Text="..."` and `Value=""` but that really makes no sense –  Jan 29 '16 at 21:07
  • 1
    Because I want to decide in controller to have blank value of not. There will be case where there will be no blank value. – ary Jan 29 '16 at 21:10
  • 1
    And I see you have just edited your question to change `ViewBag.MyList` to `ViewBag.ID`. I recommend you follow best practice and use a different name for the property your binding to and the name of the `SelectList` –  Jan 29 '16 at 21:11
  • 1
    In that case you must build your own `IEnumerable` (`SelectList` has no constructor that adds a blank (`null`) option. –  Jan 29 '16 at 21:12
  • Yes I noticed the problem in code and fixed it with proper comment.I will study your advice. Thanks for answers. This answers all. – ary Jan 29 '16 at 21:13
3

You can use this overload:

public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string optionLabel, object htmlAttributes);

where optionLabel is the text for a default empty item.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
romanoza
  • 4,775
  • 3
  • 27
  • 44
  • Can you give an example please. Also I want to add empty value " " with with id say -1 to SelectList. Thanks. – ary Jan 29 '16 at 20:56
  • 2
    For example: `@Html.DropDownList("Id", null, htmlAttributes: new { @class = "form-control", optionLabel: "[not selected]" })` If the text "[not selected]" is selected then you will get _null_ as a value. – romanoza Jan 29 '16 at 20:58
  • I am getting error" The best overloaded for "DropDownList" dosen't have a parameter named optionLabel " – ary Jan 29 '16 at 21:21
  • 2
    @ary This should compile: `@Html.DropDownList("Id", null, htmlAttributes: new { @class = "form-control"}, optionLabel: "[not selected]")` – romanoza Jan 29 '16 at 21:24
2

In my project, these work:

Controller

ViewBag.TagHfoFlagId= new SelectList(db.TableName.OrderBy(x => x.Name),"Id","Name")

View

 @Html.DropDownList("TagHfoFlagId", null,"--Select Name--", htmlAttributes: new { @id = "tags" })
RickL
  • 3,318
  • 10
  • 38
  • 39
user1575120
  • 301
  • 2
  • 4
1

The accepted answer does work, but it will show the default (null) value in the view's drop down list even if you already selected one before. If you want the already selected value to show itself in the drop down list once you render back the view, use this instead :

Controller

ViewBag.Fk_Id_Parent_Table = new SelectList(db.Parent_Table, "Id", "Name", Child_Table.Fk_Id_Parent_Table);
return View(ChildTable);

View

@Html.DropDownList(
             "Fk_Id_Parent_Table",
             null, 
             "Not Assigned", 
             htmlAttributes: new { @class = "form-control" }
                  )
Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62