填充下拉列表 [英] populating dropdown list

查看:103
本文介绍了填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数据库填充DropDown,但现在我想为特定用户预先选择值,例如DropDown显示Departments的名称,User 5在Operations部门工作,所以我输入UserID 5然后它应该在DropDown中显示预选的Department



代码:



控制器:



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





查看:



< label id =lblShiftclass =editEmp_label> Shift< / label> 
@Html.DropDownList(ShiftId,选择Shift)





我起诉Linq to sql,MVC 3 C#

解决方案

通过用户作为参数工作的DepartmentId将自动选择





DepartmentId =此方案的用户5的部门ID(操作部门的ID)

ViewData [ShiftId] =新的SelectList(ShiftNames,ShiftId,ShiftName,DepartmentId );

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

Code:

Controller:

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



View:

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



I am suing Linq to sql, MVC 3 C#

解决方案

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);


这篇关于填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆