我可以查看任何人的記錄,然後可以修改它,如果我請單擊下面的記錄編輯按鈕,點擊它帶你到另一個網頁,其中顯示文本框和下拉菜單,複選框,編輯和後所有文本框包含值是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);
}
}
這個答案可能會幫助你http://stackoverflow.com/questions/22017613/how-to-pass-id-to-default-value-of-another-view – Shyju
沒有先生它沒有 – PreciseTech