skip to Main Content

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" class="form-control" asp-item="ViewBag.employees">
                    <option selected >---Select Employee---</option>
                </select>
                <span asp-validation-for ="EmployeeId" class="text-danger"> </span>

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

2

Answers


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

    <select asp-for="EmployeeId" class="form-control" asp-items="ViewBag.employees">
      <option selected >---Select Employee---</option>
    </select>
    
    Login or Signup to reply.
  2. I hope this answer your question regards.

    <div class="row">  
        <div class="col-md-4">  
            <select id="drpEmpList" class="form-control" asp-for="EmployeeId" asp-items="@ViewBag.employees">  
                <option value="">--Select--</option>  
            </select>  
        <input type="hidden" asp-for="EmployeeId" />  
                 
    </div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search