Model
Models/Employee.cs
public class Employee
{
public int EmpId { get; set; }
public string EmpName { get; set; }
public string Dob { get; set; }
public string Mob { get; set; }
}
Models/EmployeeStore.cs
using System.Collections.Generic;
using System.Linq;
public static class EmployeeStore
{
private static readonly List<Employee> _employees = new List<Employee>
{
new Employee { EmpId = 1001, EmpName = "name1", Dob = "01/01/2005", Mob = "015444545" },
new Employee { EmpId = 1002, EmpName = "name2", Dob = "01/05/2004", Mob = "054545454" }
};
public static List<Employee> All() => _employees;
public static Employee Get(int id) => _employees.FirstOrDefault(e => e.EmpId == id);
public static int NextId() => _employees.Any() ? _employees.Max(e => e.EmpId) + 1 : 1001;
public static void Add(Employee e)
{
e.EmpId = NextId();
_employees.Add(e);
}
public static void Update(Employee e)
{
var existing = Get(e.EmpId);
if (existing != null)
{
existing.EmpName = e.EmpName;
existing.Dob = e.Dob;
existing.Mob = e.Mob;
}
}
public static void Delete(int id)
{
var e = Get(id);
if (e != null) _employees.Remove(e);
}
}
Controller
Controllers/EmployeeController.cs
using System.Web.Mvc;
public class EmployeeController : Controller
{
// LIST
public ActionResult Index()
{
return View(EmployeeStore.All());
}
// CREATE
public ActionResult Create()
{
return View(new Employee());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Employee employee)
{
if (!ModelState.IsValid) return View(employee);
EmployeeStore.Add(employee);
return RedirectToAction("Index");
}
// EDIT
public ActionResult Edit(int id)
{
var employee = EmployeeStore.Get(id);
if (employee == null) return HttpNotFound();
return View(employee);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Employee employee)
{
if (!ModelState.IsValid) return View(employee);
EmployeeStore.Update(employee);
return RedirectToAction("Index");
}
// DELETE
public ActionResult Delete(int id)
{
var employee = EmployeeStore.Get(id);
if (employee == null) return HttpNotFound();
return View(employee);
}
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
EmployeeStore.Delete(id);
return RedirectToAction("Index");
}
}
View
Views/Employee/Index.cshtml
@model IEnumerable<Employee>
<h2>Employees</h2>
@Html.ActionLink("Create New", "Create")
<table border="1" cellpadding="5">
<tr>
<th>ID</th><th>Name</th><th>Dob</th><th>Phone</th><th></th>
</tr>
@foreach (var e in Model)
{
<tr>
<td>@e.EmpId</td>
<td>@e.EmpName</td>
<td>@e.Dob</td>
<td>@e.Mob</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = e.EmpId }) |
@Html.ActionLink("Delete", "Delete", new { id = e.EmpId })
</td>
</tr>
}
</table>
Views/Employee/Create.cshtml
@model Employee
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<p>Name: @Html.TextBoxFor(m => m.EmpName)</p>
<p>Dob: @Html.TextBoxFor(m => m.Dob)</p>
<p>Phone: @Html.TextBoxFor(m => m.Mob)</p>
<input type="submit" value="Save" />
}
@Html.ActionLink("Back to List", "Index")
Views/Employee/Edit.cshtml
@model Employee
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.EmpId)
<p>Name: @Html.TextBoxFor(m => m.EmpName)</p>
<p>Dob: @Html.TextBoxFor(m => m.Dob)</p>
<p>Phone: @Html.TextBoxFor(m => m.Mob)</p>
<input type="submit" value="Save" />
}
@Html.ActionLink("Back to List", "Index")
Views/Employee/Delete.cshtml
@model Employee
<h2>Delete</h2>
<p>Are you sure you want to delete <strong>@Model.EmpName</strong>?</p>
<p>ID: @Model.EmpId | Dob: @Model.Dob | Phone: @Model.Mob</p>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.EmpId)
<input type="submit" value="Delete" />
}
@Html.ActionLink("Back to List", "Index")