Search code examples
asp.net-mvcasp.net-mvc-5

Dropdown lists first option to be null


I Have a dropdownlist that allows you to selcect an exsiting Driver ID. when the page loads the first Driver ID is already selected and I would rather allow the Driver ID dropdownlist to first have a null value selected when the page loads. How will I do this? This is my Driver ID view:

 <div class="form-group">
                @Html.LabelFor(model => model.DriverID, "Driver Cell", new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("DriverID", null, htmlAttributes: new { @class = "box" })
                    @Html.ValidationMessageFor(model => model.DriverID)
                </div>
            </div>

Solution

  • You can use

    @Html.DropDownList("DriverID", null, "Select a driver", htmlAttributes: new { @class = "box" })
    

    but this dropdown will be empty since you are passing null as SelectList.