Home > Back-end >  ASP.NET core MVC how to use dropdown list
ASP.NET core MVC how to use dropdown list

Time:10-22

Im trying to make dropdown list in my razor view using Viewbag but its not working, Im missing something I guess

public IEnumerable<SelectListItem> GetAllEmployeeForPayroll()
        {
            return GetAll().Select(emp => new SelectListItem()
            {
                Text = emp.FullName,
                Value = emp.Id.ToString()
            });
        }

ViewBag.employees = _employeeService.GetAllEmployeeForPayroll();

                <label asp-for ="EmployeeId"></label>
                <select asp-for="EmployeeId"  asp-item="ViewBag.employees">
                    <option selected >---Select Employee---</option>
                </select>
                <span asp-validation-for ="EmployeeId" > </span>

this is my code Im sure I did something wrong in razor view but cant figure out what.enter code here

CodePudding user response:

It should be asp-items but not asp-item.

<select asp-for="EmployeeId"  asp-items="ViewBag.employees">
  <option selected >---Select Employee---</option>
</select>

CodePudding user response:

I hope this answer your question regards.

<div >  
    <div >  
        <select id="drpEmpList"  asp-for="EmployeeId" asp-items="@ViewBag.employees">  
            <option value="">--Select--</option>  
        </select>  
    <input type="hidden" asp-for="EmployeeId" />  
             
</div>
  • Related