Using CRUD Operation In ASP.NET MVC 4.5 Framework

Step 1: Open Visual Studio and click File=> New=> Project.



Step 2: Now, select ASP.NET Web Application .Here, our project name will be BookManagement “.



Step 3: Here, select MVC Template and click OK button.



Step 4: Right click on App_Data and Select Add => New Item.



Step 5: In the following figure, give a database name. Here, my database name is BookDatabase.



Step 6: After creating a database, you will create a table for it.



Step 7: Now, click on Server Explorer at the left side of the Window, select BookDatabase.Mdf. Afterwards, right click Tables, Add New Table.



Step 8: I need to create some data field name and define its data type. After creating a data field name, you will click Update button. I have given the name of table Mybook.



Step 9: As given in the figure, you will click Update Database button.



Step 10: Now, I need to create a model Class. Right click on the models folder and click Add. Click Class to add a class.



Step 11: Again, select Class template and give it a name. I have given the name of Class Mybook.

Click add button.



Thus, our model class has been created. There are six properties in the class. Here, I am using validation. I need to add using System.ComponentModel.DataAnnotations; namespace for the validation.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Web;  
  6. namespace BookManagmentMVC.Models  
  7. {  
  8.     public class MyBook {  
  9.         public int Id {  
  10.             get;  
  11.             set;  
  12.         }  
  13.         [Required(ErrorMessage = "Please Enter BookName")]  
  14.         public string BookName {  
  15.             get;  
  16.             set;  
  17.         }  
  18.         [Required(ErrorMessage = "Please Enter AuthorName")]  
  19.         public string AuthorName {  
  20.             get;  
  21.             set;  
  22.         }  
  23.         [Required(ErrorMessage = "Please Enter Price")]  
  24.         public int BookPrice {  
  25.             get;  
  26.             set;  
  27.         }  
  28.         [Required(ErrorMessage = "Please Enter Bookpublish")]  
  29.         public string BookPublish {  
  30.             get;  
  31.             set;  
  32.         }  
  33.         [Required(ErrorMessage = "Please Enter Comments")]  
  34.         public string Comments {  
  35.             get;  
  36.             set;  
  37.         }  
  38.     }  
  39. }  

 

Step 12: You need to create a controller with the views, using Entity Framework. Add new Scaffold in the project.



Step 13: Now, choose MVC 5 Controller, using Entity framework.

Click Add button.



Step 14: In case of error, you will be building the project and again Add Controller.



Step 15: Finally, the controller was created by automatic CRUD operations. It is very simple to perform CRUD operations.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.Entity;  
  5. using System.Linq;  
  6. using System.Threading.Tasks;  
  7. using System.Net;  
  8. using System.Web;  
  9. using System.Web.Mvc;  
  10. namespace BookManagmentMVC.Models   
  11. {  
  12.     public class MyBooksController: Controller {  
  13.         private BookManagmentMVCContext db = new BookManagmentMVCContext();  
  14.         // GET: MyBooks    
  15.         public async Task < ActionResult > Index() {  
  16.                 return View(await db.MyBooks.ToListAsync());  
  17.             }  
  18.             // GET: MyBooks/Details/5    
  19.         public async Task < ActionResult > Details(int ? id) {  
  20.                 if (id == null) {  
  21.                     return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  22.                 }  
  23.                 MyBook myBook = await db.MyBooks.FindAsync(id);  
  24.                 if (myBook == null) {  
  25.                     return HttpNotFound();  
  26.                 }  
  27.                 return View(myBook);  
  28.             }  
  29.             // GET: MyBooks/Create    
  30.         public ActionResult Create() {  
  31.                 return View();  
  32.             }  
  33.             // POST: MyBooks/Create    
  34.             // To protect from overposting attacks, please enable the specific properties you want to bind to, for     
  35.             // more details see http://go.microsoft.com/fwlink/?LinkId=317598.    
  36.             [HttpPost]  
  37.             [ValidateAntiForgeryToken]  
  38.         public async Task < ActionResult > Create([Bind(Include = "Id,BookName,AuthorName,BookPrice,BookPublish,Comments")] MyBook myBook) {  
  39.                 if (ModelState.IsValid) {  
  40.                     db.MyBooks.Add(myBook);  
  41.                     await db.SaveChangesAsync();  
  42.                     return RedirectToAction("Index");  
  43.                 }  
  44.                 return View(myBook);  
  45.             }  
  46.             // GET: MyBooks/Edit/5    
  47.         public async Task < ActionResult > Edit(int ? id) {  
  48.                 if (id == null) {  
  49.                     return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  50.                 }  
  51.                 MyBook myBook = await db.MyBooks.FindAsync(id);  
  52.                 if (myBook == null) {  
  53.                     return HttpNotFound();  
  54.                 }  
  55.                 return View(myBook);  
  56.             }  
  57.             // POST: MyBooks/Edit/5    
  58.             // To protect from overposting attacks, please enable the specific properties you want to bind to, for     
  59.             // more details see http://go.microsoft.com/fwlink/?LinkId=317598.    
  60.             [HttpPost]  
  61.             [ValidateAntiForgeryToken]  
  62.         public async Task < ActionResult > Edit([Bind(Include = "Id,BookName,AuthorName,BookPrice,BookPublish,Comments")] MyBook myBook) {  
  63.                 if (ModelState.IsValid) {  
  64.                     db.Entry(myBook).State = EntityState.Modified;  
  65.                     await db.SaveChangesAsync();  
  66.                     return RedirectToAction("Index");  
  67.                 }  
  68.                 return View(myBook);  
  69.             }  
  70.             // GET: MyBooks/Delete/5    
  71.         public async Task < ActionResult > Delete(int ? id) {  
  72.                 if (id == null) {  
  73.                     return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  74.                 }  
  75.                 MyBook myBook = await db.MyBooks.FindAsync(id);  
  76.                 if (myBook == null) {  
  77.                     return HttpNotFound();  
  78.                 }  
  79.                 return View(myBook);  
  80.             }  
  81.             // POST: MyBooks/Delete/5    
  82.             [HttpPost, ActionName("Delete")]  
  83.             [ValidateAntiForgeryToken]  
  84.         public async Task < ActionResult > DeleteConfirmed(int id) {  
  85.             MyBook myBook = await db.MyBooks.FindAsync(id);  
  86.             db.MyBooks.Remove(myBook);  
  87.             await db.SaveChangesAsync();  
  88.             return RedirectToAction("Index");  
  89.         }  
  90.         protected override void Dispose(bool disposing) {  
  91.             if (disposing) {  
  92.                 db.Dispose();  
  93.             }  
  94.             base.Dispose(disposing);  
  95.         }  
  96.     }  
  97. }  
Step 16: You can see, here the views are automatically created.



Here is the output:



Here, click Create button. You will find all the validations are working.



In the screenshot, you will get all the information successfully inserted. If you need to Delete or Edit, you will go to the link button on Delete and Edit modify.


 


Similar Articles