articles

Home / DeveloperSection / Articles / ASP.NET MVC Scaffolding

ASP.NET MVC Scaffolding

Anonymous User6884 12-Mar-2015

Hi everyone in this article I’m explaining about Scaffolding with Asp.net mvc.

Introduction:

Scaffolding is a technique used by many MVC framework like ASP.NET MVC, node.js, cake php etc. scaffolding is used for basically generate code for CRUD operation (create, read, update, delete) operations against your database effectively. Further you can edit or customize this auto generated code according to your need.

Scaffolding consists of page templates, entity page templates, field page templates, and filter templates. These templates are called Scaffold templates and allow you to quickly build a functional data-driven Website.

How Scaffold templates works in asp.net mvc:

Scaffold templates are used to generate code for basic CRUD operation within your ASP.NET MVC application against your database with the help Entity Framework. These templates use the visual studio T4 templating system to generate view for basic CRUD operations with the help of Entity framework.

Let’s Start:

Step 1:  Open visual studio >> File >> New Project >> ASP.NET MVC 4 Web Application

ASP.NET MVC Scaffolding

Give the application name and click ok after click ok popup a window where you choose project type and click ok.

ASP.NET MVC Scaffolding

In this sample i’ll choose Internet Application.

Step 2: in the solution explorer right click models and select Add | class  to create a simple class Person name it Person and click Ok.

Open the Person class and write the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace Scaffolding.Models
{
    publicclassPerson
    {
        publicint PersonID { get; set; }
        publicstring FirstName { get; set; }
        publicstring LastName { get; set; }
    }
}

Step 3: Now Build Solution to save the changes and built the project

Step 4: In the solution explorer right click the controllers’ folder and Add Controller. Name PersonController and complete the Scaffolding options with the following values.

1.       In the Template drop-down list, select the MVC controller with read/write actions and views, using Entity Framework option.

2.       In the Model class drop-down list, select the Person class.

3.       In the Data Context class list, select <New data context...>. Choose any name and click OK.

4.       In the Views drop-down list, make sure that Razor is selected.

ASP.NET MVC Scaffolding

Click Add to create the new controller for Person with scaffolding. You have now generated the controller actions as well as the views.

ASP.NET MVC Scaffolding

 

Open PersonController class. Notice that the full CRUD action methods have been generated automatically.

using System; System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Scaffolding.Models;
 
namespace Scaffolding.Controllers
{
    publicclassPersonController : Controller
    {
        privateScaffoldingContext db = newScaffoldingContext();
 
        //
        // GET: /Person/
 
        publicActionResult Index()
        {
            return View(db.People.ToList());
        }
 
        //
        // GET: /Person/Details/5
 
        publicActionResult Details(int id = 0)
        {
            Person person = db.People.Find(id);
            if (person == null)
            {
                return HttpNotFound();
            }
            return View(person);
        }
 
        //
        // GET: /Person/Create
 
        publicActionResult Create()
        {
            return View();
        }
 
        //
        // POST: /Person/Create
 
        [HttpPost]
        publicActionResult Create(Person person)
        {
            if (ModelState.IsValid)
            {
                db.People.Add(person);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
 
            return View(person);
        }
 
        //
        // GET: /Person/Edit/5
 
        publicActionResult Edit(int id = 0)
        {
            Person person = db.People.Find(id);
            if (person == null)
            {
                return HttpNotFound();
            }
            return View(person);
        }
 
        //
        // POST: /Person/Edit/5
 
        [HttpPost]
        publicActionResult Edit(Person person)
        {
            if (ModelState.IsValid)
            {
                db.Entry(person).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(person);
        }
 
        //
        // GET: /Person/Delete/5
 
        publicActionResult Delete(int id = 0)
        {
            Person person = db.People.Find(id);
            if (person == null)
            {
                return HttpNotFound();
            }
            return View(person);
        }
 
        //
        // POST: /Person/Delete/5
 
        [HttpPost, ActionName("Delete")]
        publicActionResult DeleteConfirmed(int id)
        {
            Person person = db.People.Find(id);
            db.People.Remove(person);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
 
        protectedoverridevoid Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

 now run your application press F5

Output:


ASP.NET MVC Scaffolding

When create new user

ASP.NET MVC Scaffolding


Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By