1

i have following selectlist for dropdownbox in aspnet mvc.

This is the editEmployee Action controller, so while edit page is displayed i want to display a selectvalue in dropdownbox,since "SelectList" takes 3 parameters one for value,one for text and other is for selected value, here i'm not getting what should i pass in 3rd parameter, coz its asking an object for selected value.


ViewData["DepartmentList"] = new SelectList(DepartmentRepository.GetDepartmentsBySchoolIdInList(ViewData["schoolId"].ToString()),"DepartmentId","DepartmentTitle");

here is the view


=Html.DropDownList("DepartmentList")

1

1 Answer 1

1
var deptList = DepartmentRepository.GetDepartmentsBySchoolIdInList(ViewData["schoolId"].ToString());

ViewData["DepartmentList"] = new SelectList(DepartmentRepository.GetDepartmentsBySchoolIdInList(deptList,"DepartmentId",deptList.First());
Sign up to request clarification or add additional context in comments.

3 Comments

this is giving first item as selected value, but i want selected value as per value that the employee is assigned to department.
I gave this as an example, you asked what you pass in. You pass in the object you want selected. So if it's not first, that's fine. You could do something like deptList.Where(x => x.Dept == "IT").First()
Basically the third argument is the object you want to set the value to. So if you have an employee you could set the third argument to employee.Department

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.