我正在视图中的列表框中显示我的模型对象列表。
当我在列表框中选择特定项目并单击编辑时,我想编辑该特定模型对象。
看法 :
@Html.ListBox("Employees", (IEnumerable<SelectListItem>)ViewBag.EmployeesList )
@Html.ActionLink("Edit", "Edit", new { id=????})
我需要在运行时获取列表框选定项 id 以在操作链接中提供它。
控制器 :
public ActionResult Index()
{
var Employees= db.Employees;
ViewBag.EmployeesList = new SelectList(Employees, "EmployeeID", "EmployeeName");
return View();
}
public ActionResult Edit(int id)
{
Employee employee = db.Employees.Find(id);
return View();
}
[HttpPost]
public ActionResult Edit(Employee emp)
{
try
{
// TODO: Add update logic here
if (ModelState.IsValid)
db.Entry(emp).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View(emp);
}
}
我无法获得正确的 URL,例如:
http://127.0.0.1:81/Employee/Edit/1