Implement Insert, Update and Delete Functionality in the WebGrid: Part 2

Introduction

This article is the second part of the three article series: "Implement Insert, Update and Delete Functionality in the WebGrid". So before going through this article you must see my previous article: Implement Insert, Update and Delete Functionality in the WebGrid Part 1.

Last Part of this Series can be seen over here:-

Implement Insert, Update and Delete Functionality in the WebGrid Part 3

In my previous article we worked on the Model class of our application in which we added an ADO.NET Entity Data Model and provided some variables through which changes will be done to the database.

In this article we will work on the Controller Class of our application. This class will provide the values from the View to the Model.

Step 1

First of all add a new class to the Controller folder, this will be done by simply right-clicking on the Controller folder and then choosing "Add a New Class".

crud in webgrid 8.jpg

Step 2

After adding this class open it and add this code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MvcApplication30.Models;  
  7. namespace MvcApplication30.Controllers  
  8. {  
  9.     public class ShowDataController : Controller  
  10.     {  
  11.         //  
  12.         // GET: /ShowData/  
  13.         public ActionResult Index()  
  14.         {  
  15.             return View();  
  16.         }  
  17.         ModelServices mobjModel = new ModelServices();  
  18.         public ActionResult ShowData(int page = 1, string sort = "Student_Id"string sortDir = "ASC")  
  19.         {  
  20.             const int pageSize = 10;  
  21.             var totalRows = mobjModel.CountStudent();  
  22.             bool Dir = sortDir.Equals("desc", StringComparison.CurrentCultureIgnoreCase) ? true : false;  
  23.             var student = mobjModel.GetStudentPage(page, pageSize, sort, Dir);  
  24.             var data = new PagedStudentModel()  
  25.             {  
  26.                 TotalRows = totalRows,  
  27.                 PageSize = pageSize,  
  28.                 It_Student = student  
  29.             };  
  30.             return View(data);  
  31.         }  
  32.         [HttpGet]  
  33.         public JsonResult UpdateRecord(int id, string Student_Name, string Student_Branch, string Student_City, string Student_State)  
  34.         {  
  35.             bool result = false;  
  36.             try  
  37.             {  
  38.                 result = mobjModel.UpdateStudent(id, Student_Name, Student_Branch, Student_City, Student_State);  
  39.             }  
  40.             catch (Exception ex)  
  41.             {  
  42.             }  
  43.             return Json(new { result }, JsonRequestBehavior.AllowGet);  
  44.         }  
  45.         [HttpGet]  
  46.         public JsonResult SaveRecord(string Student_Name, string Student_Branch, string Student_City, string Student_State)  
  47.         {  
  48.             bool result = false;  
  49.             try  
  50.             {  
  51.                 result = mobjModel.SaveStudent(Student_Name, Student_Branch, Student_City, Student_State);  
  52.             }  
  53.             catch (Exception ex)  
  54.             {  
  55.             }  
  56.             return Json(new { result }, JsonRequestBehavior.AllowGet);  
  57.         }  
  58.         [HttpGet]  
  59.         public JsonResult DeleteRecord(int id)  
  60.         {  
  61.             bool result = false;  
  62.             try  
  63.             {  
  64.                 result = mobjModel.DeleteStudent(id);  
  65.             }  
  66.             catch (Exception ex)  
  67.             {  
  68.             }  
  69.             return Json(new { result }, JsonRequestBehavior.AllowGet);  
  70.         }  
  71.     }  
  72. } 

Here first I created the object of the ModelServices class that is containing the code for making changes in the database, then I created an Action Result named "ShowData", in this ShowData some varibales are passed that shows that sorting can be done only through the ID of the Student and sorting will be done in ascending order. Then the PageSize is also declared that shows that each page will contain only 10 entries, this class will pass the value for the number of rows and page size to the PagedStudentModel class and pass the data fetched from PagedStudentModel to the View Class.

After this Action Result Class, three JsonResult classes are created that will be used to update, insert and delete the records from the database.

In the Update Record class, some variables are passed for the Id, Name, Branch, City and State of the Student, these variables will hold the value that is updated in the WebGrid and will pass these values to the Update Student class of the Model through the Boolean variable "result".

Similarly, some variables are passed in the SaveRecord class that will again be passed to the Model but to a different class named SaveStudent.

At the end the delete will be done, it will simply work on the Id of the Student and will pass this ID to the DeleteStudent class of the model that will delete all the values related to that ID.

Our work on the Controller Class is done. Now only one thing is remaining to be done, the View Class, that is most an important part of this series of articles.

Next Article/ Last Part


Similar Articles