CRUD Operations With Database In MVC 5

See the following Steps:

Step 1: Open Visual Studio 2010, Go to the New Project, then Visual C#, Web and select ASP.NET MVC Web Application. After that click OK.

MVC Web Application

Step 2: After clicking OK, new ASP.NET MVC5 project window will open and there you have you chose MVC and press OK.

MVC

Step 3: After clicking OK, you will see something like the following image in your Solution Explorer. You need to look out for Model, Controller and View folders that are the main files in MVC, others are too but these are main files.

Solution Explorer

Database chamber:

Step 4: Right click on your Project, Add New Item, SQL Server Database and add it. Go to your Database, resides in Server Explorer [Database.mdf], we will create a table Student. Go to the Student Table and Add New table. Design your table like the following:

table design

Show Table Data:

table

In Model

Step 5: Right Click on Models, Add New Item, then Add ADO.NET Entity Data Model and Name it Student.edmx, then ADD it.

entity data model

Choose model contents

Connection

select table

In model

In Controller

Step 6: Right Click on Controller and Add Controller. You have to chose MVC5 Controller with Views using Entity Framework and ADD it.

Controller

Fill up the necessary model name and data context class name and add it, when you hit add, you will see, under view folder there is another folder created name – “Student” and four method created to perform – “CRUD”.

Model

Inside View Folder-

Student folder

Student Controller.cs:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.Entity;  
  5. using System.Linq;  
  6. using System.Net;  
  7. using System.Web;  
  8. using System.Web.Mvc;  
  9. using CRUDOperation.Models;  
  10.   
  11. namespace CRUDOperation.Controllers  
  12. {  
  13.     public class StudentsController : Controller  
  14.     {  
  15.         private mynewdataEntities db = new mynewdataEntities();  
  16.   
  17.         // GET: Students  
  18.         public ActionResult Index()  
  19.         {  
  20.             return View(db.Students.ToList());  
  21.         }  
  22.   
  23.         // GET: Students/Details/5  
  24.         public ActionResult Details(int? id)  
  25.         {  
  26.             if (id == null)  
  27.             {  
  28.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  29.             }  
  30.             Student student = db.Students.Find(id);  
  31.             if (student == null)  
  32.             {  
  33.                 return HttpNotFound();  
  34.             }  
  35.             return View(student);  
  36.         }  
  37.   
  38.         // GET: Students/Create  
  39.         public ActionResult Create()  
  40.         {  
  41.             return View();  
  42.         }  
  43.   
  44.         // POST: Students/Create  
  45.         // To protect from overposting attacks, please enable the specific properties you want to bind to, for   
  46.         // more details see http://go.microsoft.com/fwlink/?LinkId=317598.  
  47.         [HttpPost]  
  48.         [ValidateAntiForgeryToken]  
  49.         public ActionResult Create([Bind(Include = "ID,Name,City")] Student student)  
  50.         {  
  51.             if (ModelState.IsValid)  
  52.             {  
  53.                 db.Students.Add(student);  
  54.                 db.SaveChanges();  
  55.                 return RedirectToAction("Index");  
  56.             }  
  57.   
  58.             return View(student);  
  59.         }  
  60.   
  61.         // GET: Students/Edit/5  
  62.         public ActionResult Edit(int? id)  
  63.         {  
  64.             if (id == null)  
  65.             {  
  66.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  67.             }  
  68.             Student student = db.Students.Find(id);  
  69.             if (student == null)  
  70.             {  
  71.                 return HttpNotFound();  
  72.             }  
  73.             return View(student);  
  74.         }  
  75.   
  76.         // POST: Students/Edit/5  
  77.         // To protect from overposting attacks, please enable the specific properties you want to bind to, for   
  78.         // more details see http://go.microsoft.com/fwlink/?LinkId=317598.  
  79.         [HttpPost]  
  80.         [ValidateAntiForgeryToken]  
  81.         public ActionResult Edit([Bind(Include = "ID,Name,City")] Student student)  
  82.         {  
  83.             if (ModelState.IsValid)  
  84.             {  
  85.                 db.Entry(student).State = EntityState.Modified;  
  86.                 db.SaveChanges();  
  87.                 return RedirectToAction("Index");  
  88.             }  
  89.             return View(student);  
  90.         }  
  91.   
  92.         // GET: Students/Delete/5  
  93.         public ActionResult Delete(int? id)  
  94.         {  
  95.             if (id == null)  
  96.             {  
  97.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  98.             }  
  99.             Student student = db.Students.Find(id);  
  100.             if (student == null)  
  101.             {  
  102.                 return HttpNotFound();  
  103.             }  
  104.             return View(student);  
  105.         }  
  106.   
  107.         // POST: Students/Delete/5  
  108.         [HttpPost, ActionName("Delete")]  
  109.         [ValidateAntiForgeryToken]  
  110.         public ActionResult DeleteConfirmed(int id)  
  111.         {  
  112.             Student student = db.Students.Find(id);  
  113.             db.Students.Remove(student);  
  114.             db.SaveChanges();  
  115.             return RedirectToAction("Index");  
  116.         }  
  117.   
  118.         protected override void Dispose(bool disposing)  
  119.         {  
  120.             if (disposing)  
  121.             {  
  122.                 db.Dispose();  
  123.             }  
  124.             base.Dispose(disposing);  
  125.         }  
  126.     }  
  127. }  
Output

Here you can create, edit , delete or update the data – Try it.

Output

 


Similar Articles