Hi When I use the following for my dropdown
@Html.DropDownListFor(x => x.OIC_2, new SelectList(ViewBag.StaffMembers, "Value", "Text"))
I am getting the following error even though there is data in the viewbag for staff members
Viewbag.StaffMembers is made up from
public void GetStaffMemebers() {
List<SelectListItem> listItems = new List<SelectListItem>();
var items = _context.StaffMember.Where(w=>w.isAcitve ==true && w.isDeleteted ==false).ToList();
foreach (var item in items) {
SelectListItem listItem = new SelectListItem();
listItem.Text = item.FirstName + " " + item.LastName ;
listItem.Value = item.Id.ToString();
listItems.Add(listItem);
}
ViewBag.StaffMembers = listItems;
}
Error
ArgumentNullException: Value cannot be null. (Parameter 'items') Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList..ctor(IEnumerable items, string dataValueField, string dataTextField, IEnumerable selectedValues, string dataGroupField) Microsoft.AspNetCore.Mvc.Rendering.SelectList..ctor(IEnumerable items, string dataValueField, string dataTextField, object selectedValue) Microsoft.AspNetCore.Mvc.Rendering.SelectList..ctor(IEnumerable items, string dataValueField, string dataTextField) CallSite.Target(Closure , CallSite , Type , object , string , string ) System.Dynamic.UpdateDelegates.UpdateAndExecute4<T0, T1, T2, T3, TRet>(CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3) AspNetCore.Views_MISObjects_Edit.b__27_0() in Edit.cshtml + @Html.DropDownListFor(x => x.OIC_2, new SelectList(ViewBag.StaffMembers, "Value", "Text"))
GetStaffMemebersmethod from controller action? - Chetan