Skip to content

Test #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 63 additions & 22 deletions Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,100 @@ public class UserController : Controller
{
public static System.Collections.Generic.List<User> userlist = new System.Collections.Generic.List<User>();
// 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");
}

}
}