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:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using MvcApplication30.Models;

 

namespace MvcApplication30.Controllers

{

    public class ShowDataController : Controller

    {

        

        //

        // GET: /ShowData/

 

        public ActionResult Index()

        {

            return View();

        }

        ModelServices mobjModel = new ModelServices();

        public ActionResult ShowData(int page = 1, string sort = "Student_Id"string sortDir = "ASC")

        {

            const int pageSize = 10;

            var totalRows = mobjModel.CountStudent();

 

            bool Dir = sortDir.Equals("desc"StringComparison.CurrentCultureIgnoreCase) ? true : false;

 

            var student = mobjModel.GetStudentPage(page, pageSize, sort, Dir);

            var data = new PagedStudentModel()

            {

                TotalRows = totalRows,

                PageSize = pageSize,

                It_Student = student

            };

            return View(data);

        }

 

        [HttpGet]

        public JsonResult UpdateRecord(int id, string Student_Name, string Student_Branch, string Student_City, string Student_State)

        {

            bool result = false;

            try

            {

                result = mobjModel.UpdateStudent(id, Student_Name, Student_Branch, Student_City, Student_State);

 

            }

            catch (Exception ex)

            {

            }

            return Json(new { result }, JsonRequestBehavior.AllowGet);

        }

 

        [HttpGet]

        public JsonResult SaveRecord(string Student_Name, string Student_Branch, string Student_City, string Student_State)

        {

            bool result = false;

            try

            {

                result = mobjModel.SaveStudent(Student_Name, Student_Branch, Student_City, Student_State);

 

            }

            catch (Exception ex)

            {

            }

            return Json(new { result }, JsonRequestBehavior.AllowGet);

        }

 

        [HttpGet]

        public JsonResult DeleteRecord(int id)

        {

            bool result = false;

            try

            {

                result = mobjModel.DeleteStudent(id);

 

            }

            catch (Exception ex)

            {

            }

            return Json(new { result }, JsonRequestBehavior.AllowGet);

        }

    }

}

 

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

Up Next
    Ebook Download
    View all
    Learn
    View all