A list of in-controller students gets reset after I edit it within my Post method.
I debugged to make sure the edit method was actually changing the list and it was. As soon as I RedirectToAction("Index") the list of students goes back to the original.
public class StudentController : Controller
{
private List<Student> students = new List<Student>()
{
new Student() { Id = 0, Name = "John", Age = 18 },
new Student() { Id = 1, Name = "Steve", Age = 21 },
new Student() { Id = 2, Name = "Bill", Age = 25 },
new Student() { Id = 3, Name = "Ram", Age = 20 },
new Student() { Id = 4, Name = "Ron", Age = 31 },
new Student() { Id = 5, Name = "Chris", Age = 17 },
new Student() { Id = 6, Name = "Rob", Age = 19 },
};
public IActionResult Index()
{
return View(students);
}
public IActionResult Edit(int id)
{
Student std = students.Where(s => s.Id == id).FirstOrDefault();
return View(std);
}
[HttpPost]
public IActionResult Edit(Student std)
{
students[std.Id] = std;
return RedirectToAction("Index");
}
}