MVC CRUD Step By Step

MVC CRUD 

In this article we will learn CRUD (create, read ,update,delete) operation using Asp.net MVC, C#, SQL Server , Entity Framework (DB First Approach). . So i will show you all steps of MVC Project Creation. 

Steps overview

Creating Project => Adding EDM => Database Setting =>Adding Controller => Routing


Step 1. Open Visual Studio => Click on File => New Project

Step 2 : Adding EDM










Step 3: Add Controller



Just modify entries depending on you. (My entries are shown below.)


Open "CRUDcontroller" Controller from solution files.

  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 CRUDoperations.Controllers  
  10. {  
  11.     public class CRUDController : Controller  
  12.     {  
  13.         private TestDBEntities db = new TestDBEntities();  
  14.   
  15.         //  
  16.         // GET: /CRUD/  
  17.   
  18.         public ActionResult Index()  
  19.         {  
  20.             return View(db.EMPLOYEEs.ToList());  
  21.         }  
  22.   
  23.         //  
  24.         // GET: /CRUD/Details/5  
  25.   
  26.         public ActionResult Details(string id = null)  
  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: /CRUD/Create  
  38.   
  39.         public ActionResult Create()  
  40.         {  
  41.             return View();  
  42.         }  
  43.   
  44.         //  
  45.         // POST: /CRUD/Create  
  46.   
  47.         [HttpPost]  
  48.         [ValidateAntiForgeryToken]  
  49.         public ActionResult Create(EMPLOYEE employee)  
  50.         {  
  51.             if (ModelState.IsValid)  
  52.             {  
  53.                 db.EMPLOYEEs.Add(employee);  
  54.                 db.SaveChanges();  
  55.                 return RedirectToAction("Index");  
  56.             }  
  57.   
  58.             return View(employee);  
  59.         }  
  60.   
  61.         //  
  62.         // GET: /CRUD/Edit/5  
  63.   
  64.         public ActionResult Edit(string id = null)  
  65.         {  
  66.             EMPLOYEE employee = db.EMPLOYEEs.Find(id);  
  67.             if (employee == null)  
  68.             {  
  69.                 return HttpNotFound();  
  70.             }  
  71.             return View(employee);  
  72.         }  
  73.   
  74.         //  
  75.         // POST: /CRUD/Edit/5  
  76.   
  77.         [HttpPost]  
  78.         [ValidateAntiForgeryToken]  
  79.         public ActionResult Edit(EMPLOYEE employee)  
  80.         {  
  81.             if (ModelState.IsValid)  
  82.             {  
  83.                 db.Entry(employee).State = EntityState.Modified;  
  84.                 db.SaveChanges();  
  85.                 return RedirectToAction("Index");  
  86.             }  
  87.             return View(employee);  
  88.         }  
  89.   
  90.         //  
  91.         // GET: /CRUD/Delete/5  
  92.   
  93.         public ActionResult Delete(string id = null)  
  94.         {  
  95.             EMPLOYEE employee = db.EMPLOYEEs.Find(id);  
  96.             if (employee == null)  
  97.             {  
  98.                 return HttpNotFound();  
  99.             }  
  100.             return View(employee);  
  101.         }  
  102.   
  103.         //  
  104.         // POST: /CRUD/Delete/5  
  105.   
  106.         [HttpPost, ActionName("Delete")]  
  107.         [ValidateAntiForgeryToken]  
  108.         public ActionResult DeleteConfirmed(string id)  
  109.         {  
  110.             EMPLOYEE employee = db.EMPLOYEEs.Find(id);  
  111.             db.EMPLOYEEs.Remove(employee);  
  112.             db.SaveChanges();  
  113.             return RedirectToAction("Index");  
  114.         }  
  115.   
  116.         protected override void Dispose(bool disposing)  
  117.         {  
  118.             db.Dispose();  
  119.             base.Dispose(disposing);  
  120.         }  
  121.     }  

NOw open View. 


Step 4:  Routing

Open Route.Config.CS file from APP_start folder.
In my case I changed it to Controller = “CRUD”.

Now just run the project you will see List of record As follow. 


Is there any confusion or issue please write on comment section . 
MVC CRUD Step By Step MVC CRUD Step By Step Reviewed by Ashwani on August 30, 2018 Rating: 5

No comments:

Powered by Blogger.