Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC - Clear default selected value of SelectList

Tags:

c#

asp.net-mvc

I have a SelectList that gets populated with data from the database. The data comes from a Dictionary<int, string> and populates the SelectList like below.

//...

Courses = new SelectList(GetCourses(),"Key","Value");

//...

On the View, it shows the first record from the database as the default selected value on the dropdown. I'd like to clear this value and leave the dropdown empty so the user can manually select a value. If possible without using javascript.

//I have this on the view
<%= Html.DropDownListFor(model => model.CourseID, Model.Courses) %>

Please help!

like image 703
Nick Masao Avatar asked Jan 22 '23 09:01

Nick Masao


1 Answers

Use the overload which takes an optionLabel:

<%= Html.DropDownListFor(model => model.CourseID, Model.Courses, string.Empty) %>
like image 105
Craig Stuntz Avatar answered Jan 24 '23 00:01

Craig Stuntz