Search code examples
c#asp.net-coredrop-down-menudropdownasp.net-core-2.1

How to make drop-down default to blank


When I go to my web page, it has a drop down list with a name already selected. I want that it to default to a blank value every time someone goes to that page. They will need to type in that blank space.

<select asp-for="OwnerId" class="form-control" asp-items="@Model.OwnerList"></select>

Solution

  • Use append/prepend or appendTo/prependTo to add new empty option to the beginning or end of the list with jquery.

    Make sure to run the script after document loaded:

    $(function () {
        $("#OwnerId").prepend('<option></option>').val('');
    });
    

    or

    $(function () {
        $('<option>', { value: '' }).appendTo($("#OwnerId"));
    });