tanyaa tanyaa - 7 days ago 8
ASP.NET (C#) Question

how to delete in asp.net without from a static list(not from database)?

I'm new with asp.net. I'm trying to build a small project, where I have a static list of drivers and their details. I'm trying to delete but its not going to well. can someone please check my delete functions?

public ActionResult DeleteDriver(int id)
{

var driver = _drivers.Single(d => d.id == id);
_drivers.Remove(driver);
return View(driver);

}

// POST: Drivers/Delete/5
[HttpPost]
public ActionResult DeleteDriver(int id, FormCollection collection)
{
var driver = _drivers.Single(d => d.id == id);
if (TryUpdateModel(driver))
{
//...
return RedirectToAction("index");

}
return View(driver);
}

Answer

Remove the driver from returned View. You can return to index page with a list of drivers or you can return an empty View.

var driver = _drivers.Single(d => d.id == id);
_drivers.Remove(driver);
return View();