Load (Populate) DropDownList from Controller in ASP.Net MVC

on Feb 10, 2020 06:31 PM

Hello everyone

How to load Dropdownlist value in select from controller where value and text are different.

i am just simply trying to load values in a dropdownlist from a controller.

if I have both the value and text be the same values it works with the model property.

But i am trying to have the value be the id and the text to be the name with this setup the dropdown is not loading the id.

If both text and value are the same I can just do this and it works

@Html.DropDownListFor(e => e.ServiceVM.Employee, Model.Employees, "Select Employee", new { @class = "form-control" })


vm.Employee = new SelectListItem { Value = model.EmployeeID.ToString(), Selected=true };
Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Feb 11, 2020 08:38 AM

I really dont understand why this isn't selecting the dropdownlist value.

Iam setting the correct 'value' and 'text' that are displayed in the list of ddl items and also saying selected=true.

            if (model.EmployeeID != null)
                if(Int32.TryParse(model.EmployeeID.ToString(), out int empID))
                    var employee = context.Employee_Main.Where(e => e.EmployeeID == empID).FirstOrDefault();
                    if(employee != null)
                        vm.Employee = new SelectListItem() { Value = employee.EmployeeID.ToString(), Text = employee.EFullName, Selected=true };


@Html.DropDownListFor(e => e.ServiceVM.Employee, Model.Employees, "Select Employee", new { @class = "form-control" })

I got it working. I forgot to change my javascript to match with the changing model fields.

var employ = modalA.find(body).find("#ServiceVM_Employee");