Project Name: StudentManagementMVC
Overview:
- Add student
- View list of students
- Edit student
- Delete student
Folder Structure:
StudentManagementMVC/
■■■ Controllers/
■ ■■■ StudentController.cs
■■■ Models/
■ ■■■ Student.cs
■■■ Views/
■ ■■■ Student/
■ ■■■ Index.cshtml
■ ■■■ Create.cshtml
■ ■■■ Edit.cshtml
■ ■■■ Delete.cshtml
■■■ App_Start/
■ ■■■ RouteConfig.cs
■■■ Global.asax
■■■ Web.config
-----------------------------------------------------
Model: Student.cs
-----------------------------------------------------
namespace StudentManagementMVC.Models
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Course { get; set; }
}
}
-----------------------------------------------------
Controller: StudentController.cs
-----------------------------------------------------
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using StudentManagementMVC.Models;
namespace StudentManagementMVC.Controllers
{
public class StudentController : Controller
{
static List<Student> students = new List<Student>();
public ActionResult Index()
{
return View(students);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Student s)
{
s.Id = students.Count + 1;
students.Add(s);
return RedirectToAction("Index");
}
public ActionResult Edit(int id)
{
var s = students.FirstOrDefault(x => x.Id == id);
return View(s);
}
[HttpPost]
public ActionResult Edit(Student updated)
{
var s = students.FirstOrDefault(x => x.Id == updated.Id);
if (s != null)
{
s.Name = updated.Name;
s.Course = updated.Course;
}
return RedirectToAction("Index");
}
public ActionResult Delete(int id)
{
var s = students.FirstOrDefault(x => x.Id == id);
return View(s);
}
[HttpPost]
public ActionResult Delete(Student s)
{
students.RemoveAll(x => x.Id == s.Id);
return RedirectToAction("Index");
}
}
}
-----------------------------------------------------
Views
Index.cshtml
-----------------------------------------------------
@model List<StudentManagementMVC.Models.Student>
@{
ViewBag.Title = "Student List";
}
<h2>Student List</h2>
<p>@Html.ActionLink("Add New", "Create")</p>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Course</th>
<th>Actions</th>
</tr>
@foreach (var s in Model)
{
<tr>
<td>@s.Id</td>
<td>@s.Name</td>
<td>@s.Course</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = s.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = s.Id })
</td>
</tr>
}
</table>
-----------------------------------------------------
Create.cshtml
-----------------------------------------------------
@model StudentManagementMVC.Models.Student
@{
ViewBag.Title = "Add Student";
}
<h2>Add Student</h2>
@using (Html.BeginForm())
{
<p>Name: @Html.TextBoxFor(x => x.Name)</p>
<p>Course: @Html.TextBoxFor(x => x.Course)</p>
<input type="submit" value="Save" />
}
-----------------------------------------------------
Edit.cshtml
-----------------------------------------------------
@model StudentManagementMVC.Models.Student
@{
ViewBag.Title = "Edit Student";
}
<h2>Edit Student</h2>
@using (Html.BeginForm())
{
@Html.HiddenFor(x => x.Id)
<p>Name: @Html.TextBoxFor(x => x.Name)</p>
<p>Course: @Html.TextBoxFor(x => x.Course)</p>
<input type="submit" value="Update" />
}
-----------------------------------------------------
Delete.cshtml
-----------------------------------------------------
@model StudentManagementMVC.Models.Student
@{
ViewBag.Title = "Delete Student";
}
<h2>Delete Student</h2>
@using (Html.BeginForm())
{
@Html.HiddenFor(x => x.Id)
<p>Are you sure you want to delete <b>@Model.Name</b>?</p>
<input type="submit" value="Yes, Delete" />
}
-----------------------------------------------------
Run Instructions:
- Open Visual Studio
- New Project → ASP.NET Web Application (.NET Framework)
- Choose MVC Template
- Add above files and run (F5)