Scaffolding in Asp.Net MVC 4 with Crud Operations Example

Scaffolding is a technique used by many MVC frameworks like ASP.NET MVC, Ruby on Rails, Cake PHP and Node.JS, etc. to generate code for basic CRUD (create, read, update, and delete) operations against your database effectively. Further, you can edit or customize this auto-generated code according to your need.

CRUD Operations in database using Scaffolding

It is simple to create CRUD operations using Scaffolding in asp.net MVC already Microsoft provides convention over configuration. Let start with adding controller in the previous project repository pattern in asp.net mvc because in that project we already created the required database model using the ado.net entity model. 

 

Adding controller with name “HoildaysController” and while adding in Template select “Mvc controller with empty read/write actions and views, using Entity Framework” this will create a whole structure and code of CRUD method for you. In Model class select EmployeeDetails Model class and in Data context Class Select EmployeeDBEntites which is your context class like as shown below 

 

Select scaffolding options in asp.net mvc application

 

After selecting all Details finally click on Add button. Here is snapshot of complete project which contains HoildaysController and also Related Views of it which are auto Generated by Scaffolding option.

 

scaffolding options controller in asp.net mvc application with crud operations

 

After this here is complete code of HoildaysController. This code is similar to EmployeeRepository in previous project respository pattern in mvc but in this we are writing whole code in Action Method which is not proper for Maintenance and your Action Method will get heavy with code.

 

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.Entity;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using MVCrepo.Models;

 

namespace MVCrepo.Controllers

{

public class HoildaysController : Controller

{

private EmployeeDBEntities db = new EmployeeDBEntities();

//

// GET: /Hoildays/

public ActionResult Index()

{

return View(db.EmployeeDetails.ToList());

}

//

// GET: /Hoildays/Details/5

public ActionResult Details(int id = 0)

{

EmployeeDetail employeedetail = db.EmployeeDetails.Find(id);

if (employeedetail == null)

{

return HttpNotFound();

}

return View(employeedetail);

}

//

// GET: /Hoildays/Create

publicActionResult Create()

{

return View();

}

//

// POST: /Hoildays/Create

[HttpPost]

public ActionResult Create(EmployeeDetail employeedetail)

{

if (ModelState.IsValid)

{

db.EmployeeDetails.Add(employeedetail);

db.SaveChanges();

return RedirectToAction("Index");

}

return View(employeedetail);

}

//

// GET: /Hoildays/Edit/5

public ActionResult Edit(int id = 0)

{

EmployeeDetail employeedetail = db.EmployeeDetails.Find(id);

if (employeedetail == null)

{

return HttpNotFound();

}

return View(employeedetail);

}

//

// POST: /Hoildays/Edit/5

[HttpPost]

public ActionResult Edit(EmployeeDetail employeedetail)

{

if (ModelState.IsValid)

{

db.Entry(employeedetail).State = EntityState.Modified;

db.SaveChanges();

return RedirectToAction("Index");

}

return View(employeedetail);

}

//

// GET: /Hoildays/Delete/5

 

public ActionResult Delete(int id = 0)

{

EmployeeDetail employeedetail = db.EmployeeDetails.Find(id);

if (employeedetail == null)

{

return HttpNotFound();

}

return View(employeedetail);

}

//

// POST: /Hoildays/Delete/5

[HttpPost, ActionName("Delete")]

public ActionResult DeleteConfirmed(int id)

{

EmployeeDetail employeedetail = db.EmployeeDetails.Find(id);

db.EmployeeDetails.Remove(employeedetail);

db.SaveChanges();

return RedirectToAction("Index");

}

protected override void Dispose(bool disposing)

{

db.Dispose();

base.Dispose(disposing);

}

}

}

Now just run application and check HolidayController our result will be like as shown below 

Index Page in Scaffolding

Scaffolding user details page in asp.net mvc application

Edit Page in Scaffolding

Scaffolding user edit page in asp.net mvc application

Details Page in Scaffolding

Scaffolding user details page in asp.net mvc application

Remaining all Views are similar. We have not wrote single line of code for this Process everything already build we just used those features in our asp.net mvc application. We finished Performing CRUD operation in database using Scaffolding.