tags:

views:

51

answers:

1

hi, 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  A: 
var deptList = DepartmentRepository.GetDepartmentsBySchoolIdInList(ViewData["schoolId"].ToString());

ViewData["DepartmentList"] = new SelectList(DepartmentRepository.GetDepartmentsBySchoolIdInList(deptList,"DepartmentId",deptList.First());
taylonr
@taylonr, this is giving first item as selected value, but i want selected value as per value that the employee is assigned to department.
FosterZ
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()
taylonr
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
taylonr
@taylonr: thank u.
FosterZ