MVC Scaffolding with Example - beginners

/
0 Comments

ASP.NET MVC scaffolding provides a quick way to generate the CRUD operations in a standardized way, creating the necessary logic that lets your application interact with the database layer. in this post, explained with example for how to use scaffolding.

Create new employee table with existing/new sql database

We created new table Emplyoyee in sql DB and this table will be used for Scaffolding.

Create New Project

created the project with name MvcScaffolding with basic template

Create the edmx file

Step 1

Step 2

Step 3

Step 4

Step 5

Step 6

This edmx created and included newly created employee table also.we will use this edmx for scaffolding

Create the controller

created the new controller with name Employee and used the template read/write actions,views using entity framework

After Created the controller using scaffolding

Index.cshtml file

Code Snippet
  1. @model IEnumerable<MvcScaffolding.Employee>
  2.  
  3. @{
  4.     ViewBag.Title = "Index";
  5. }
  6.  
  7. <h2>Index</h2>
  8.  
  9. <p>
  10.     @Html.ActionLink("Create New", "Create")
  11. </p>
  12. <table>
  13.     <tr>
  14.         <th>
  15.             @Html.DisplayNameFor(model => model.EmpId)
  16.         </th>
  17.         <th>
  18.             @Html.DisplayNameFor(model => model.EmployeeName)
  19.         </th>
  20.         <th>
  21.             @Html.DisplayNameFor(model => model.Position)
  22.         </th>
  23.         <th>
  24.             @Html.DisplayNameFor(model => model.CityID)
  25.         </th>
  26.         <th></th>
  27.     </tr>
  28.  
  29. @foreach (var item in Model) {
  30.     <tr>
  31.         <td>
  32.             @Html.DisplayFor(modelItem => item.EmpId)
  33.         </td>
  34.         <td>
  35.             @Html.DisplayFor(modelItem => item.EmployeeName)
  36.         </td>
  37.         <td>
  38.             @Html.DisplayFor(modelItem => item.Position)
  39.         </td>
  40.         <td>
  41.             @Html.DisplayFor(modelItem => item.CityID)
  42.         </td>
  43.         <td>
  44.             @Html.ActionLink("Edit", "Edit", new {  id=item.EmpId  }) |
  45.             @Html.ActionLink("Details", "Details", new {id=item.EmpId }) |
  46.             @Html.ActionLink("Delete", "Delete", new { id=item.EmpId })
  47.         </td>
  48.     </tr>
  49. }
  50.  
  51. </table>

Details.cshtml file

Code Snippet
  1. @model MvcScaffolding.Employee
  2.  
  3. @{
  4.     ViewBag.Title = "Details";
  5. }
  6.  
  7. <h2>Details</h2>
  8.  
  9. <fieldset>
  10.     <legend>Employee</legend>
  11.  
  12.     <div class="display-label">
  13.          @Html.DisplayNameFor(model => model.EmpId)
  14.     </div>
  15.     <div class="display-field">
  16.         @Html.DisplayFor(model => model.EmpId)
  17.     </div>
  18.  
  19.     <div class="display-label">
  20.          @Html.DisplayNameFor(model => model.EmployeeName)
  21.     </div>
  22.     <div class="display-field">
  23.         @Html.DisplayFor(model => model.EmployeeName)
  24.     </div>
  25.  
  26.     <div class="display-label">
  27.          @Html.DisplayNameFor(model => model.Position)
  28.     </div>
  29.     <div class="display-field">
  30.         @Html.DisplayFor(model => model.Position)
  31.     </div>
  32.  
  33.     <div class="display-label">
  34.          @Html.DisplayNameFor(model => model.CityID)
  35.     </div>
  36.     <div class="display-field">
  37.         @Html.DisplayFor(model => model.CityID)
  38.     </div>
  39. </fieldset>
  40. <p>
  41.     @Html.ActionLink("Edit", "Edit", new { /* id=Model.PrimaryKey */ }) |
  42.     @Html.ActionLink("Back to List", "Index")
  43. </p>

Employee Controller

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8.  
  9. namespace MvcScaffolding.Controllers
  10. {
  11.     public class EmployeeController : Controller
  12.     {
  13.         private TestDBEntities db = new TestDBEntities();
  14.  
  15.         //
  16.         // GET: /Employee/
  17.  
  18.         public ActionResult Index()
  19.         {
  20.             return View(db.Employees.ToList());
  21.         }
  22.  
  23.         //
  24.         // GET: /Employee/Details/5
  25.  
  26.         public ActionResult Details(int id = 0)
  27.         {
  28.             Employee employee = db.Employees.Find(id);
  29.             if (employee == null)
  30.             {
  31.                 return HttpNotFound();
  32.             }
  33.             return View(employee);
  34.         }
  35.  
  36.         //
  37.         // GET: /Employee/Create
  38.  
  39.         public ActionResult Create()
  40.         {
  41.             return View();
  42.         }
  43.  
  44.         //
  45.         // POST: /Employee/Create
  46.  
  47.         [HttpPost]
  48.         public ActionResult Create(Employee employee)
  49.         {
  50.             if (ModelState.IsValid)
  51.             {
  52.                 db.Employees.Add(employee);
  53.                 db.SaveChanges();
  54.                 return RedirectToAction("Index");
  55.             }
  56.  
  57.             return View(employee);
  58.         }
  59.  
  60.         //
  61.         // GET: /Employee/Edit/5
  62.  
  63.         public ActionResult Edit(int id = 0)
  64.         {
  65.             Employee employee = db.Employees.Find(id);
  66.             if (employee == null)
  67.             {
  68.                 return HttpNotFound();
  69.             }
  70.             return View(employee);
  71.         }
  72.  
  73.         //
  74.         // POST: /Employee/Edit/5
  75.  
  76.         [HttpPost]
  77.         public ActionResult Edit(Employee employee)
  78.         {
  79.             if (ModelState.IsValid)
  80.             {
  81.                 db.Entry(employee).State = EntityState.Modified;
  82.                 db.SaveChanges();
  83.                 return RedirectToAction("Index");
  84.             }
  85.             return View(employee);
  86.         }
  87.  
  88.         //
  89.         // GET: /Employee/Delete/5
  90.  
  91.         public ActionResult Delete(int id = 0)
  92.         {
  93.             Employee employee = db.Employees.Find(id);
  94.             if (employee == null)
  95.             {
  96.                 return HttpNotFound();
  97.             }
  98.             return View(employee);
  99.         }
  100.  
  101.         //
  102.         // POST: /Employee/Delete/5
  103.  
  104.         [HttpPost, ActionName("Delete")]
  105.         public ActionResult DeleteConfirmed(int id)
  106.         {
  107.             Employee employee = db.Employees.Find(id);
  108.             db.Employees.Remove(employee);
  109.             db.SaveChanges();
  110.             return RedirectToAction("Index");
  111.         }
  112.  
  113.         protected override void Dispose(bool disposing)
  114.         {
  115.             db.Dispose();
  116.             base.Dispose(disposing);
  117.         }
  118.     }
  119. }

Views (Create,Details,Edit,Index ,Delete) and controlller(Employee) codes are automatically cretaed for CRUD operations.

Run the project

Manage/Index

Create

Edit

Details

Delete

The CRUD operations are working fine after using scaffolding. by this way we can save time while develop the application.



You may also like

No comments:

Powered by Blogger.