0

I cannot manage to put a placeholder titled "Please Select", so I simply put it as an item in my drop-down, but now "Please select" can be selected, which is a bit of a nuisance. Is there a way to have a place holder for the drop-down list below.

 <div class="form-group">
                @Html.LabelFor(model => model.Type,  htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(model => model.Type, new List<SelectListItem>      
                        { new SelectListItem{Text="Please select type"},
                          new SelectListItem{Text="Book"},
                          new SelectListItem{Text="Book chapter"},
                          new SelectListItem{Text="Journal article"},
                          new SelectListItem{Text="Conference"}}, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })

                </div>
            </div>
Kivan Ilangakoon
  • 457
  • 1
  • 10
  • 24
  • 1
    possible duplicate of [@Html.DropDownListFor how to set default value](http://stackoverflow.com/questions/23799091/html-dropdownlistfor-how-to-set-default-value) – Jesse Sep 22 '15 at 12:56
  • It makes no sense to have a 'placholder' for a select. The correct approach is to use the overload of `DropDownListFor()` that accepts a `optionLabel` and remove the first `SelectListItem` in your collection - `@Html.DropDownListFor(m => m.Type, new List {...}, "Please Select", new { @class = "form" })` which create the first option with a `null` value –  Sep 22 '15 at 13:02

1 Answers1

-1

Overloads 4, 5 & 6 of the DropDownListFor helper all contain a parameter optionLabel, which can be used for your purposes.

Frayt
  • 1,194
  • 2
  • 17
  • 38
  • do you mind explaining what you mean by overloads 4, 5 and 6. Sorry I am very new to MVC – Kivan Ilangakoon Sep 22 '15 at 16:11
  • Take a look here: https://msdn.microsoft.com/en-us/library/vstudio/ms229029(v=vs.100).aspx. When typing Html.DropDownListFor you should get a list of possible parameter inputs coming up. The items 4, 5, and 6 should have an `optionLabel` parameter of type string. – Frayt Sep 23 '15 at 07:02