2014-02-26 78 views
0

我可以查看任何人的记录,然后可以修改它,如果我请单击下面的记录编辑按钮,点击它带你到另一个网页,其中显示文本框和下拉菜单,复选框,编辑和后所有文本框包含值是EmpID,但问题是,我想在下拉列表中选择前值,喜欢的话应该选择相应的值预选喜欢我的文本框每个用户。填充从数据库使用MVC 3下拉,LINQ to SQL的

我使用LINQ到SQL,ASP.NET MVC 3和C#

代码

@using EmployeeAttendance_app.Models 
@model IEnumerable<GetEmployeeEditDetails_SpResult> 

@{ 
    var Item = Model.FirstOrDefault(); 
} 

<style type="text/css"> 

</style> 
<div> 
@using (Html.BeginForm("EditEmployee", "Home", FormMethod.Get)) 
{ 
    <label id="lblName" class="editEmp_label">Name</label> 
    <input type="text" value= @Item.EmplName name="EmpName" placeholder="Update Name" /> 
    <br /> 
    <label id="lblDept" class="editEmp_label">Department</label> 
    @Html.DropDownList("DeptID", @Item.DeptName) 
    <br /> 
    <label id="lblShift" class="editEmp_label">Shift</label> 
    @Html.DropDownList("ShiftId") 

控制器:

public ActionResult EditEmployee() 
{ 
    if (!String.IsNullOrEmpty(Session["Admin"] as string)) 
    { 
     int? EmplId = Convert.ToInt32(Session["EmpEdit"]); 
     IEnumerable<GetEmployeeEditDetails_SpResult> EmployeeValues = DataContext.GetEmployeeEditDetails_Sp(EmplId).ToList(); 
     var DepNames = (from n in DataContext.HrDepts select new { n.DeptID, n.DeptName }).Distinct(); 
     ViewData["DeptID"] = new SelectList(DepNames, "DeptID", "DeptName"); 
     var ShiftNames = (from n in DataContext.AtdShifts select new { n.ShiftId, n.ShiftName }).Distinct(); 
     ViewData["ShiftId"] = new SelectList(ShiftNames, "ShiftId", "ShiftName"); 
     return View(EmployeeValues); 
    } 
} 
+0

这个答案可能会帮助你http://stackoverflow.com/questions/22017613/how-to-pass-id-to-default-value-of-another-view – Shyju

+0

没有先生它没有 – PreciseTech

回答

0

如果你想使一个为DropDownList预定义的选定值,最简单的方法是在SelectList()中定义选定的值。因此,你可以简单地做这在你的控制器:

ViewData["DeptID"] = 
    new SelectList(DepNames, 
    "DeptID", // Value 
    "DeptName", // Text 
    EmployeeValues.FirstOrDefault().DepId); // selected value 

另外,在你看来,你需要使用@ Html.DropDownList这样的:

@Html.DropDownList("DeptID", (SelectList)ViewData["DeptID"]) 

我建议你读多一点的文档或使用html帮助程序类时的intellisense提示。

+0

不工作,相同的结果。 (“DeptID”,(SelectList)ViewData [“DeptID”]) new cdoe: @DefaultID(“DepptName” .DeptName); – PreciseTech

+0

你会得到什么样的结果?下拉列表是否没有任何选择?或者选择不是默认的你想要的? – tweray