Skip to main content

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 . 

Comments

Popular posts from this blog

MVC Interview Questions

MVC Interview Questions What is MVC Application life cycle ? What is default route in MVC? What is Output Caching ? What are Route Constraints in MVC? What are the Benefits of Area in MVC? How do you implement Forms authentication in MVC? What are the Main Razor Syntax Rules? Define attribute based routing in MVC? What are HTML helpers in MVC? What does the MVC pattern define with 3 logical layers? What are the advantages of MVC? List the various return types of a controller action method. What are the types of filters? What are the Filters in MVC? What are the three segments for routing important? What is Route in MVC? what is the difference between ViewData and ViewBag? attribute based routing in MVC? What is GET and POST Actions Types? What is MVC (Model view controller)? What is Validation Summary in MVC? What’s new in the latest version of MVC? How to render html in Asp.net MVC view? Explain the concept of MVC Scaffolding? What is Bundling and Mini...

Angular Step By Step Installation Process

# Angular Step By Step  Hello Techies,        In this post i am going to guide you to install Angular and create and run your first app. Please follow bellow steps.. Step 1 (Node NPM Installation) Node Js ( NPM)  should be installed on system. So for installation of Node Js follow bellow steps. Open  https://nodejs.org/en  .  Download stable Version (LTS) of Node js. After complete the installation open CMD and check the version. Step : 2 (Install Angular by NPM) Type " npm install -g @angular/cli "  2.  Select folder where you are going to create Angular Project.  3. Type ng new 'your app-name' EX:  ng new my-dream-app And Relax it will take some couple of minute to create app , because its downloading all the package from server by npm in you local system. Type : " cd my-dream-app " to select project which is created.  Now Type " ng serve " to run the project...

Azure Service Bus Vs Azure Web Job

MENU Home SUBSCRIBE Today, we will be talking about Azure service bus and how to use them with Webjobs. Azure Service Bus  is a messaging infrastructure that sits between applications allowing them to exchange messages for improved scale and resiliency. It offers the following capabilities: Queues: offers simple first in, first out guaranteed message delivery. When to use Azure Service Bus? Service Bus queues are a general-purpose technology that can be used for a wide variety of scenarios: Communication between web and worker roles in a multi-tier Azure application Communication between on-premises apps and Azure hosted apps in a hybrid solution Communication between components of a distributed application running on-premises in different organizations or departments of an organization There are also Topics and Relay capabiliteis but we will talk about them in the future. A figure that illustrates how Azure Service Bus works. There is a sender who send a mes...