Introduction
CRUD operation in MVC is the basic operations, where CRUD denotes create, read, update, and delete. But before understanding the basic operations of MVC, first, learn about MVC. MVC is the Model View Controller. MVC is a design pattern that is used to differentiate the data from business logic and presentation logic. It gives a pattern that helps in designing the web application.
Check out our free courses to get an edge over the competition.
MVC divides the application into three layers, as described below:
1. Model Layer: MVC has a model component that deals with logic-related data. The model layer represents the information transferred between view and controller layers of data related to the business logic. For example, employee objects help fetch the employee information from the relevant table in the database, manipulate the data, and then update it back into the database.
2. View Layer: The view layer has the view components that deal with the User interface logic. As an illustration, an employee’s view components comprise the components, such as text boxes, radio buttons, drop-downs, check-boxes, etc. The view layer has the components that the end-user deals with.
3. Controller Layer: Controller is the interface between view layer components and model layer components. The controller controls the business logic. It receives the user input through the view layer and processes the information through the model layer.
The processed information is then returned to the user by the controller through the view layer.
To exemplify, an employee wants to see the salary-related information. The employee can enter the details through UI components in view; the controller then retrieves the relevant information from the model layer and displays the information to the employee’s view layer to see it.
The interaction between the MVC layers can be easily understood using the below diagram:
These were the basics of MVC. Now, coming back to our original topic, the basic CRUD operation in MVC. Below are the CRUD operations:
- Create a record in the database
- Read a record in the database
- Update a record in the database
- Delete a record in the database
Explore Our Software Development Free Courses
Steps to Create an MVC Project
1. First, create an MVC application. Click on Start on your PC, and then click on All Programs.
2. Click on Microsoft Visual Studio 2015.
3. Click on File > New > Project and select ASP.NET Web Application Template.
4. Enter the project name and click Ok.
5. Click on Empty, check the check-box MVC, and click on Ok. An empty MVC web application will open.
6. Right-click on the Models folder, then add the option, and then the class option.
Below is the code snippet for the class called Employee1.cs:
public class Employee1
{
[Display(Name = “EmpId”)]
public int Empid { get; set; }
[Required(ErrorMessage = “First name is required.”)]
public string FName { get; set; }
[Required(ErrorMessage = “City is required.”)]
public string City { get; set; }
[Required(ErrorMessage = “Address is required.”)]
public string Address { get; set; }
}
7. The next step is adding a controller. Select MVC5 Controller with read/write a class and click on the Add button. Enter the controller name.
Explore our Popular Software Engineering Courses
CRUD Operation in MVC
1. Create a Record in the Database
Create a new record in the new controller using the below code snippet:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CRUDDemo.Controllers
{
public class CRUDController : Controller
{
// To create View of this Action result
public ActionResult create()
{
return View();
}
// Specify the type of attribute i.e.
// it will add the record to the database
[HttpPost]
public ActionResult create(Employee1 emp)
{
If (ModelState.IsValid)
{
Db.Employee1.Add(emp);
Db.SaveChanges();
Return RedirectToAction(“Index”);
}
Return View(emp);
}
}
2. Read the Record From the Database
Below is the code to read the record from the database
public ActionResult Read()
{
using(var context = new demoCRUDEntities())
{
// Return the data from database
var data = context.Employee1.ToList();
return View(data);
}
}
Read: Exception Handling in MVC
3. Update a Record in the Database
Below is the code to edit or update the record in the database:
public ActionResult Edit(string id = null)
{
Employee1 emp = db.Employee1.Find(id);
if (emp == null)
{
return HttpNotFound();
}
return View(emp);
}
[HttpPost]
public ActionResult Edit(Employee1 emp)
{
if (ModelState.IsValid)
{
db.Entry(emp).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction(“Index”);
}
return View(emp);
}
In-Demand Software Development Skills
4. Delete the Record From the Database
Below is the code snippet to delete the record from the database:
public ActionResult Delete(string id = null)
{
Employee1emp = db.Employee1.Find(id);
if (emp == null)
{
return HttpNotFound();
}
return View(emp);
}
[HttpPost, ActionName(“Delete”)]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(string id)
{
Employee1 emp = db.Employee1.Find(id);
db.Employee1.Remove(emp);
db.SaveChanges();
return RedirectToAction(“Index”);
}
Read our Popular Articles related to Software Development
Why Learn to Code? How Learn to Code? | How to Install Specific Version of NPM Package? | Types of Inheritance in C++ What Should You Know? |
Conclusion
CRUD is the most basic operations of MVC used in ASP.net. I hope the CRUD operation in MVC is clear to you now, and you can try implementing this code to perform the CRUD operations.
If you are interested to know more about Big Data, check out our Advanced Certificate Programme in Big Data from IIIT Bangalore.
Check our other Software Engineering Courses at upGrad.