How to change textbox to dropdown list in asp.net mvc?

Viewed 79

I have the following code in asp.net mvc where it is a textbox and I want to change it to a Dropdown list.

text box in asp.net mvc

 <div class="form-group">
        @Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
        </div>
    </div>

TextBox

<div class="form-group">
        @Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
           <select asp-for="name" asp-items="@Model.name" class="form-control"> </select>
            @Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
        </div>
    </div>
1 Answers

You must replace

@Html.EditorFor(...

for this

<label asp-for="SupervisorId"></label>
<select asp-for="SupervisorId" asp-items="@Model.Supervisors" class="form-control">
</select>
<input name="SupervisorId" type="hidden" />
<span asp-validation-for="SupervisorId" class="text-danger"></span>
Related