Home > Software design >  How to change textbox to dropdown list in asp.net mvc?
How to change textbox to dropdown list in asp.net mvc?

Time:09-23

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

TextBox

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

CodePudding user response:

You must replace

@Html.EditorFor(...

for this

<label asp-for="SupervisorId"></label>
<select asp-for="SupervisorId" asp-items="@Model.Supervisors" >
</select>
<input name="SupervisorId" type="hidden" />
<span asp-validation-for="SupervisorId" ></span>
  • Related