Home > DeveloperSection > Beginner > ASP.NET MVC Scaffolding

ASP.NET MVC Scaffolding


ASP.NET MVC C#  ASP.Net  Mvc  Mvc4 
Ratings:
0 Comment(s)
 1833  View(s)
Rate this:

ASP.NET MVC Scaffolding

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

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

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

{

    public class Person

    {

        public int PersonID { get; set; }

        public string FirstName { get; set; }

        public string 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.

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

 

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

using System;

using 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

{

    public class PersonController : Controller

    {

        private ScaffoldingContext db = new ScaffoldingContext();

 

        //

        // GET: /Person/

 

        public ActionResult Index()

        {

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

        }

 

        //

        // GET: /Person/Details/5

 

        public ActionResult Details(int id = 0)

        {

            Person person = db.People.Find(id);

            if (person == null)

            {

                return HttpNotFound();

            }

            return View(person);

        }

 

        //

        // GET: /Person/Create

 

        public ActionResult Create()

        {

            return View();

        }

 

        //

        // POST: /Person/Create

 

        [HttpPost]

        public ActionResult Create(Person person)

        {

            if (ModelState.IsValid)

            {

                db.People.Add(person);

                db.SaveChanges();

                return RedirectToAction("Index");

            }

 

            return View(person);

        }

 

        //

        // GET: /Person/Edit/5

 

        public ActionResult Edit(int id = 0)

        {

            Person person = db.People.Find(id);

            if (person == null)

            {

                return HttpNotFound();

            }

            return View(person);

        }

 

        //

        // POST: /Person/Edit/5

 

        [HttpPost]

        public ActionResult Edit(Person person)

        {

            if (ModelState.IsValid)

            {

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

                db.SaveChanges();

                return RedirectToAction("Index");

            }

            return View(person);

        }

 

        //

        // GET: /Person/Delete/5

 

        public ActionResult 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")]

        public ActionResult DeleteConfirmed(int id)

        {

            Person person = db.People.Find(id);

            db.People.Remove(person);

            db.SaveChanges();

            return RedirectToAction("Index");

        }

 

        protected override void Dispose(bool disposing)

        {

            db.Dispose();

            base.Dispose(disposing);

        }

    }

}

 

now run your application press F5

Output:

When create new user


Don't want to miss updates? Please click the below button!

Follow MindStick