如何将HttpPost请求传递的Student对象存入本地List集合?
To persist your submitted Student records across requests without a database, you need a storage mechanism that survives controller instantiations (since controllers are created fresh for each request). Below are two practical approaches tailored to different use cases:
Approach 1: Static List (Shared Global Dataset)
A static list keeps data alive as long as your application runs, but it’s shared between all users. This is the simplest option if you want a single, global collection of student entries.
Updated Controller Code
public class StudentsController : Controller { // Static list to retain data across controller instances private static List<Student> dbTempStudentList = new List<Student>(); [HttpPost] public ActionResult Create(Student _student) { if (ModelState.IsValid) { // Add the submitted student to our in-memory collection dbTempStudentList.Add(_student); // Redirect to a view showing all stored students return RedirectToAction("Index"); } // If validation fails, return to the form with the submitted data return View(_student); } // Optional: Action to display all stored students public ActionResult Index() { return View(dbTempStudentList); } }
Approach 2: Session Storage (Per-User Isolation)
Using Session ensures each user has their own independent list of students. This is better for multi-user apps where data should be isolated per session.
Updated Controller Code
public class StudentsController : Controller { [HttpPost] public ActionResult Create(Student _student) { if (ModelState.IsValid) { // Retrieve the user's list from Session, or create a new one if it doesn't exist List<Student> dbTempStudentList = Session["StudentList"] as List<Student> ?? new List<Student>(); // Add the new student to the list dbTempStudentList.Add(_student); // Save the updated list back to Session Session["StudentList"] = dbTempStudentList; return RedirectToAction("Index"); } return View(_student); } // Action to display the user's stored students public ActionResult Index() { // Get the user's list from Session (or empty list if none exists) var studentList = Session["StudentList"] as List<Student> ?? new List<Student>(); return View(studentList); } }
Note: Session is enabled by default in ASP.NET MVC, but if you’ve disabled it, you’ll need to re-enable it in your application configuration.
Student Entity Class (Unchanged)
Your existing Student class works perfectly for this use case:
public class Student { public int StudentID { get; set; } public string StudentName { get; set; } public string StudentLastName { get; set; } public string Creditbalance { get; set; } public string CurrentBalance { get; set; } }
Add an Index View to Display Stored Students
To visualize the stored data, create an Index.cshtml view in the Views/Students folder:
@{ ViewBag.Title = "Stored Students"; } <h2>Stored Students</h2> <p> @Html.ActionLink("Add New Student", "Create") </p> <table class="table"> <thead> <tr> <th>@Html.DisplayNameFor(model => model.StudentID)</th> <th>@Html.DisplayNameFor(model => model.StudentName)</th> <th>@Html.DisplayNameFor(model => model.StudentLastName)</th> <th>@Html.DisplayNameFor(model => model.Creditbalance)</th> <th>@Html.DisplayNameFor(model => model.CurrentBalance)</th> </tr> </thead> <tbody> @foreach (var student in Model) { <tr> <td>@Html.DisplayFor(item => student.StudentID)</td> <td>@Html.DisplayFor(item => student.StudentName)</td> <td>@Html.DisplayFor(item => student.StudentLastName)</td> <td>@Html.DisplayFor(item => student.Creditbalance)</td> <td>@Html.DisplayFor(item => student.CurrentBalance)</td> </tr> } </tbody> </table>
Key Notes
- Data Persistence: Both approaches store data in memory, so all entries will be lost if your application restarts (e.g., during deployment or server reset).
- Static List: Use this only if you want a shared, global dataset accessible to all users.
- Session: Ideal for scenarios where each user should manage their own independent set of student entries.
内容的提问来源于stack exchange,提问作者Maria Perez

