在我的Post方法中对其进行编辑后,会重置一个处于控制状态的学生列表。
我进行了调试,以确保edit方法实际上在更改列表,并且确实如此。一旦我RedirectToAction(“ Index”),学生列表就会回到原始列表。
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");
}
}