Home > database >  .NET Core MVC - DropDownList from Enumerable produces an "multiple" - How to remove?
.NET Core MVC - DropDownList from Enumerable produces an "multiple" - How to remove?

Time:01-10

I have a Site with a Row "Employees", which has eight Dropdowns. To these Dropdowns I have bound a ViewBag with all the Employees and the same List Property. THe problem with this is that it will produce an "multiple" HTML output, where I can select multiple Employees and the whole Dropdown is showing as a List.

Basically:

<div >
  <div >
      <select asp-for="Employees" asp-items="ViewBag.Employees"  >       </select>
   </div>
   <div >
      <select asp-for="Employees" asp-items="ViewBag.Employees" >       </select>
   .......
                

In the Model:

public ICollection<int?>? Employees { get; set; }

When I run this Page: 1.) The Dropdown will be "multiple select" and 2.) The Dropdown is shown like a List. It shows all the Employees openly without clicking on it.

Can I somehow prevent this without declaring eight "Employee 1-8" Properties?

CodePudding user response:

Because your type is ICollection<int?>?, So asp-for will generate multiple="multiple" property, You just need to use name="Employees" instead of asp-for="Employees" in this case.

Code:

<div >
     <select asp-for="Employees" asp-items="ViewBag.Employees" >       </select>
</div>
<div >
     <select name="Employees" asp-items="ViewBag.Employees" >       </select>
</div>

View:

enter image description here

  • Related