Introduction to Scaffolding in Visual Studio 2013 RC

Rama Sagar
Posted by in ASP.NET MVC category on for Beginner level | Points: 250 | Views : 8625 red flag

Scaffolding is a Code Generation Framework used to perform Crud Operations…It generates the code for create, read, update and delete operations in your application..we can save time developing standard types of Web Applications. We use Scaffolding to add moderate code to interact the data model

Introduction


This Article explains you to how to use Scaffolding Technique in your MVC 5 Application



Scaffolding...?


  •  It is a Code Generation Framework used to perform Crud Operations…
  •  It will generate a basic outline of your application which you can edit and customize…
  • It gives us the basic layout using that layout we can customize our application instead of starting from scratch




Lets look into the Sample Implementation



Note: Before beginning please create a MVC 5 Application and add a class in the model. Which i had already explained in my previous article you can grab it from here..

  • Step 1 Now right click on the controller folder in your MVC Application to add a scaffold as shown below .
       

  • Step 2 Now Select the Option (MVC 5 Controller with views ,using Entity Framework) as shown below 
   


 You can notice Add scaffold wizard is different in this version webform scaffolding is removed in this verison 

  • Step 3 Now Enter the Controller name ,Model class and data context class as shown below...
  




Now you can watch the Generated StudentsController class file with created CRUD operations automatically and the views  as shown below
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MVCStudents.Models;

namespace MVCStudents.Controllers
{
    public class StudentsController : Controller
    {
        private StudentsDbcontext db = new StudentsDbcontext();

        // GET: /Students/
        public ActionResult Index()
        {
            return View(db.Students.ToList());
        }

        // GET: /Students/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Students students = db.Students.Find(id);
            if (students == null)
            {
                return HttpNotFound();
            }
            return View(students);
        }

        // GET: /Students/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Students/Create
		// To protect from over posting attacks, please enable the specific properties you want to bind to, for 
		// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
		// 
		// Example: public ActionResult Update([Bind(Include="ExampleProperty1,ExampleProperty2")] Model model)
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Students students)
        {
            if (ModelState.IsValid)
            {
                db.Students.Add(students);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(students);
        }

        // GET: /Students/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Students students = db.Students.Find(id);
            if (students == null)
            {
                return HttpNotFound();
            }
            return View(students);
        }

        // POST: /Students/Edit/5
		// To protect from over posting attacks, please enable the specific properties you want to bind to, for 
		// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
		// 
		// Example: public ActionResult Update([Bind(Include="ExampleProperty1,ExampleProperty2")] Model model)
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Students students)
        {
            if (ModelState.IsValid)
            {
                db.Entry(students).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(students);
        }

        // GET: /Students/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Students students = db.Students.Find(id);
            if (students == null)
            {
                return HttpNotFound();
            }
            return View(students);
        }

        // POST: /Students/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Students students = db.Students.Find(id);
            db.Students.Remove(students);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}




Now Debug the Application and create some Student Details as shown


Conclusion


We have learned the new scaffolding Methodology and how to use it in latest version of visual studio 2013 RC..Thanks for having a look at my article..

Reference

http://msdn.microsoft.com/en-us/library/dd405231(v=vs.100).aspx

Page copy protected against web site content infringement by Copyscape

About the Author

Rama Sagar
Full Name: RamaSagar Pulidindi
Member Level: Silver
Member Status: Member,MVP
Member Since: 12/30/2012 1:51:40 AM
Country: India
ramasagar
http://www.ramasagar.com
A Software Profesional working in Microsoft .NET technologies since year 2008, and I work for Dake ACE. I am passionate about .NET technology and love to contribute to the .NET community at Dot Net Funda

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)