I am displaying list of my model objects in a Listbox in my view.
When I select a particular item in the Listbox and click edit, I would like to edit that particular Model object.
View :
@Html.ListBox("Employees", (IEnumerable<SelectListItem>)ViewBag.EmployeesList )
@Html.ActionLink("Edit", "Edit", new { id=????})
I need to get the list box selected item id at runtime to provide it in the actionlink.
Controller :
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);
}
}
I am not able to get the right URL like:
http://127.0.0.1:81/Employee/Edit/1