2

I have a question in my MVC application which requires a dropdown list and I want to add a "Please select" option to the dropdown to hide the answers until it is clicked.

@Html.DropDownListFor(model => model.QuestionTwo, new SelectList
(new[]{"A","B", "C", "D"}))
@Html.ValidationMessageFor(model => model.QuestionTwo)

Because of the way I have created my list of answers, I'm not sure how to add a default "Please Select" value that cannot be selected by the user.

Vogel612
  • 5,620
  • 5
  • 48
  • 73
Cooper1810
  • 31
  • 10
  • 1
    `@Html.DropDownListFor(model => model.QuestionTwo, new SelectList (new[]{"A","B", "C", "D"}),"Please Select")`. Here you go. – Suprabhat Biswal Dec 10 '15 at 12:43
  • You can do it by JavaScript or adding an empty value in your list at the beginning or extend DropDownListFor method ... What are the constraints ? – D4rkTiger Dec 10 '15 at 13:39
  • 5
    Or you can also see this post http://stackoverflow.com/questions/18153328/mvc-4-how-to-make-dropdownlistfor-return-0-as-optionlabel-value – D4rkTiger Dec 10 '15 at 13:42
  • Adding Please Select to the end of the selectList will not work because it means that can be accepted as an answer, I want it to say Please Select but not allow that to be selected. – Cooper1810 Dec 10 '15 at 14:15

1 Answers1

0

a good way to do this is like

<option value="" disabled selected>Select something...</option>

I found it on this thread How do I make a placeholder for a 'select' box?

Best bet would be to use jQuery to add this option ad the top

$('select').append('<option value="" disabled selected>Please Select</option>');
Community
  • 1
  • 1
Ian
  • 334
  • 2
  • 11