CRUD Operation In ASP.NET MVC Using Angular With Basics - Part Two

This is part two of CRUD operation in ASP MVC using Angular. Before starting, I recommend you read my previous article about Angular and its features and versions. To read, please click here.
 
We have covered the basic things of Angular and its features, versions, folder structures, and usage of Angular configuration files in our previous article. In this article, we are going to create a Database and a Controller to perform CRUD operation using Angular.
 
Let's create a simple employee database and create employee table to save our records.
 
 
 
I have named our employee table as "EmployeeDetails". Here, EmpId is a primary key and we have set it as an identity column.
 
Please find some sample insert scripts here.
  1.   Insert into [EmployeeDetails](EmpName,EmpAge,EmpCity,EmpMobileNo)VALUES('Ramesh',27,'Bangalore','1234567890');  
  2.   Insert into [EmployeeDetails](EmpName,EmpAge,EmpCity,EmpMobileNo)VALUES('Marry',23,'Chennai','1234567890');  
  3.   Insert into [EmployeeDetails](EmpName,EmpAge,EmpCity,EmpMobileNo)VALUES('Sushanth',27,'Delhi','1234567890');  
  4.   Insert into [EmployeeDetails](EmpName,EmpAge,EmpCity,EmpMobileNo)VALUES('vijay',27,'Chennai','1234567890');  
We have already created a project "AngularSampleDemo"  and included all the required Angular files in our previous article. We are going to continue with this same project in this tutorial.
 
 
 
Let's first add Entity model to our project. To add, please follow the below steps.
 
Select "Data Model" folder = > Right Click -> Add -> New Item -> Select "Ado.Net Entity Model".
 
 
 
 
 
 
 
 
Now, you can see that our entity model has been added to our data model folder. Let's add Employee Controller to our project under Controller folder.
 


 
 
Copy the below code and paste it in our employee controller.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using AngularSampleDemo.DataModel;  
  7.   
  8. namespace AngularSampleDemo.Controllers  
  9. {  
  10.     public class EmployeeController : Controller  
  11.     {  
  12.           
  13.         // GET: Employee  
  14.         public ActionResult Index()  
  15.         {  
  16.             return View();  
  17.         }  
  18.          [HttpGet]
  19.         public JsonResult GetAllEmployees()  
  20.         {  
  21.             using (EmployeeEntities entities = new EmployeeEntities())  
  22.             {  
  23.                 List<EmployeeDetail> employeeList = new List<EmployeeDetail>();  
  24.                 employeeList = entities.EmployeeDetails.ToList();  
  25.                 return Json(employeeList, JsonRequestBehavior.AllowGet);  
  26.             }  
  27.              
  28.         }  
  29.         [HttpGet]
  30.         public JsonResult GetEmployee(int EmpId)  
  31.         {  
  32.             using (EmployeeEntities entities = new EmployeeEntities())  
  33.             {  
  34.                 EmployeeDetail _employee = entities.EmployeeDetails.Where(x => x.EmpId == EmpId).Select(x => x).FirstOrDefault();  
  35.                 return Json(_employee, JsonRequestBehavior.AllowGet);  
  36.             }  
  37.         }  
  38.         [HttpPost]
  39.         public JsonResult InsertEmployee(EmployeeDetail employeeDetail)  
  40.         {  
  41.             using (EmployeeEntities entities = new EmployeeEntities())  
  42.             {  
  43.                 entities.EmployeeDetails.Add(employeeDetails);  
  44.                 entities.SaveChanges();  
  45.             }  
  46.             return Json(true, JsonRequestBehavior.AllowGet);  
  47.         }  
  48.       [HttpPost]
  49.         public JsonResult UpdateEmployee(EmployeeDetail employeeDetail)  
  50.         {  
  51.             using (EmployeeEntities entities = new EmployeeEntities())  
  52.             {  
  53.                 EmployeeDetail _employee = entities.EmployeeDetails.Where(x => x.EmpId == employeeDetails.EmpId).Select(x => x).FirstOrDefault();  
  54.                 _employee.EmpName = employeeDetails.EmpName;  
  55.                 _employee.EmpCity = employeeDetails.EmpCity;  
  56.                 _employee.EmpAge = employeeDetails.EmpAge;  
  57.                 entities.SaveChanges();  
  58.             }  
  59.             return Json(true, JsonRequestBehavior.AllowGet);  
  60.         }  
  61.        [HttpGet]
  62.         public JsonResult DeleteEmployee(int EmpId)  
  63.         {  
  64.             using (EmployeeEntities entities = new EmployeeEntities())  
  65.             {  
  66.                 EmployeeDetail _employee = entities.EmployeeDetails.Where(x => x.EmpId == EmpId).Select(x => x).FirstOrDefault();  
  67.                 entities.EmployeeDetails.Remove(_employee);  
  68.                 entities.SaveChanges();  
  69.             }  
  70.             return Json(true, JsonRequestBehavior.AllowGet);  
  71.         }  
  72.     }  
  73. }  
Let's understand the code.
  • GetAllEmployees() method is used to return the list of employees from our EmployeeDetails table. 
  • GetEmployee(int EmpId) method is used to return the particular employee from our EmployeeDetails table using employee Id(EmpId)
  • InsertEmployee(EmployeeDetail employeeDetail) method is used to insert the employee information in our employee details table.
  • UpdateEmployee(EmployeeDetail employeeDetail) method is used to update the employee information in our employee details table using employee Id.
  • DeleteEmployee(int EmpId) method is used to delete the employee information from employee details table using emp Id.
Note
  1. All the action results are going to return the data as JSON data.
  2. EmployeeEntities is our entity DB context to access employee table informations. 
To know more about entity context and Entity Model, please refer to the below articles. Definitely, you will get some basic idea about it.
Please provide your valuable comments to improve my writting. Thanks for reading.
 


Similar Articles