ITWorker ITWorker - 1 month ago 5x
ASP.NET (C#) Question

How to default Html.DropDownList to no selection?

I have the following dropdown populated by a ViewBag element:

@Html.DropDownList("SelectedRole", ViewBag.Roles as List<SelectListItem>, new { @class = "form-control" })

Currently, upon page load, the first element of the Roles list is displayed, however I want to default it to no selection (the dropdown should be blank on page load). My attempt was in jQuery as follows, but it did not have any effect on the dropdown default selection (still showed the first element of Roles):

$("#SelectedRole").prop("selectedIndex", -1);

Is there a way I could set the default within Razor itself? If not, through some other way?

Thank you.


Use this syntax

@Html.DropDownList("SelectedRole", ViewBag.Roles as List<SelectListItem>,"", new { @class = "form-control" })

Note: I have added a "" in the syntax.. this will be the first option in the dropdown. Hope this helps..