diff --git a/Controllers/UserController.cs b/Controllers/UserController.cs index f9ecbda2..f5cb892c 100644 --- a/Controllers/UserController.cs +++ b/Controllers/UserController.cs @@ -8,59 +8,100 @@ public class UserController : Controller { public static System.Collections.Generic.List userlist = new System.Collections.Generic.List(); // GET: User + + public ActionResult Index() { - // Implement the Index method here + return View(userlist); } - + // GET: User/Details/5 public ActionResult Details(int id) { - // Implement the details method here + User user = userlist.Find(u => u.Id == id); + if (user == null) + { + return HttpNotFound(); + } + return View(user); } - + + // GET: User/Create public ActionResult Create() { - //Implement the Create method here + return View(); } - + // POST: User/Create [HttpPost] public ActionResult Create(User user) { - // Implement the Create method (POST) here + if (ModelState.IsValid) + { + userlist.Add(user); + return RedirectToAction("Index"); + } + return View(user); } - + + + + // GET: User/Edit/5 // GET: User/Edit/5 public ActionResult Edit(int id) { - // This method is responsible for displaying the view to edit an existing user with the specified ID. - // It retrieves the user from the userlist based on the provided ID and passes it to the Edit view. + User user = userlist.Find(u => u.Id == id); + if (user == null) + { + return HttpNotFound(); + } + return View(user); } - + // POST: User/Edit/5 [HttpPost] public ActionResult Edit(int id, User user) { - // This method is responsible for handling the HTTP POST request to update an existing user with the specified ID. - // It receives user input from the form submission and updates the corresponding user's information in the userlist. - // If successful, it redirects to the Index action to display the updated list of users. - // If no user is found with the provided ID, it returns a HttpNotFoundResult. - // If an error occurs during the process, it returns the Edit view to display any validation errors. + if (ModelState.IsValid) + { + var existingUser = userlist.Find(u => u.Id == id); + if (existingUser != null) + { + // Update the existing user's data + existingUser.Name = user.Name; + existingUser.Email = user.Email; + // Add other properties as needed + + return RedirectToAction("Index"); + } + } + return View(user); } - + + // GET: User/Delete/5 public ActionResult Delete(int id) { - // Implement the Delete method here + User user = userlist.Find(u => u.Id == id); + if (user == null) + { + return HttpNotFound(); + } + return View(user); } - + // POST: User/Delete/5 - [HttpPost] - public ActionResult Delete(int id, FormCollection collection) + [HttpPost, ActionName("Delete")] + public ActionResult DeleteConfirmed(int id) { - // Implement the Delete method (POST) here + User user = userlist.Find(u => u.Id == id); + if (user != null) + { + userlist.Remove(user); + } + return RedirectToAction("Index"); } + } }