Click here to Skip to main content
15,350,151 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am populating DropDown from database but now i want pre selected value for specific user, e.g. DropDown shows names of Departments, and User 5 works in Operations departments so i enter as UserID 5 then it should show preselected Department in DropDown



var ShiftNames = (from n in DataContext.AtdShifts select new { n.ShiftId, n.ShiftName }).Distinct();
                ViewData["ShiftId"] = new SelectList(ShiftNames, "ShiftId", "ShiftName");


<label id="lblShift" class="editEmp_label">Shift</label>
  @Html.DropDownList("ShiftId", "Select Shift")

I am suing Linq to sql, MVC 3 C#
JoCodes 26-Feb-14 3:06am
Why cant you pass the UserId as parameter to the Linq Query and use it in the Where condition?
Kornfeld Eliyahu Peter 26-Feb-14 3:11am
Why in the first place you show a combo for a user? Can he change his department? It seems like an administrator privilege...You may think about different design for simple user and admin user...

1 solution

Pass the DepartmentId in which user works as a parameter will automatically select

DepartmentId= Department Id of user 5 for this scenario (Id of Operation Department)
ViewData["ShiftId"] = new SelectList(ShiftNames, "ShiftId", "ShiftName",DepartmentId);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900