Actually I needed to implement it in my project and I encountered many difficulties in getting this feature because I hardly found any help for it and if I found something then it was just halfway useful.

I recommend you to visit this before proceeding with this article.

This is a second part of Integration of jqGrid with MVC4 application. This part is more about enabling add, edit, delete and refresh as well as a little validation using builtin validation keywords.

To learn more about MVC kindly go to C# Corner MVC Section or My Blog.

I've developed this sample application using Visual Studio 2012 and used Entity Framework to retrieve information from the database.

Step 1 : Enabling jqGrid add, edit, delete and refresh



After enabling values we should run the application and press F5. Kindly see the following image.




I have pasted this URL into a browser http://localhost:26158/Admin to get the above result.

Here is the complete code of AddTopic.cshtml, though I will share the AdminController class file and AddTopic.cshtml file with you as a sample so that you could add it into your solution.

Note: the code segment of jqGrid in the AddTopic.cshtml view is as shown below:

  1. @{
  2. ViewBag.Title = "ADDTOPICS";
  3. Layout = "~/Views/Shared/_Layout.cshtml";
  4. }
  5. <!DOCTYPE html>
  6. <html>
  7. <head>
  8. <meta name="viewport" content="width=device-width" />
  9. <title>AddTopics</title>
  10. <script src="~/Scripts/jquery.jqGrid.js"></script>
  11. <script src="~/Scripts/jquery.jqGrid.min.js"></script>
  12. <script src="~/Scripts/i18n/grid.locale-en.js"></script>
  13. <link href="~/Content/jquery.jqGrid/ui.jqgrid.css" rel="stylesheet" />
  14. <link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
  15. <!-- The jqGrid client-side javascript -->
  16. @* <script type="text/javascript" src="http://www.trirand.net/aspnetmvc/Scripts/trirand/jquery.jqGrid.min.js"></script>*@
  17. <script type="text/javascript">
  18. var ClickEventId;
  19. $(document).ready(function myfunction() {
  20. $('#list').jqGrid({
  21. caption: "Add Topics",
  22. url: '/Admin/GetTopics/',
  23. datatype: "json",
  24. contentType: "application/json; charset-utf-8",
  25. mtype: 'GET',
  26. colNames: ['ID', 'Name', 'Created_by', 'Created_On'],
  27. colModel: [
  28. { name: 'ID', index: 'ID', width: 150, editable: true, editoptions: { readonly: 'readonly' }, editrules: { readonly: true } },
  29. {
  30. name: 'topic_name', index: 'topic_name', width: 150, editable: true, editrules: { required: true }
  31. },
  32. {
  33. name: 'Created_by', index: 'Created_by', width: 150, editable: true, editrules: { required: true }
  34. },
  35. { name: 'Created_On', index: 'Created_On', width: 150, editable: true, editrules: { required: true } }
  36. ],
  37. rowNum: 10,
  38. loadonce: true,
  39. pager: '#pager',
  40. editurl: '/Admin/EditTopics/'
  41. });
  42. jQuery("#list").jqGrid('navGrid', '#pager', {
  43. edit: true,
  44. add: true,
  45. del: true,
  46. search: true,
  47. refresh: true,
  48. searchtext: "Search",
  49. addtext: "Add",
  50. edittext: "Edit",
  51. deltext: "Delete",
  52. refreshtext:"Refresh"
  53. }
  54. );
  55. $('.ui-pg-button').on('click', function (ev) {
  56. ClickEventId = this.id;
  57. });
  58. });
  59. </script>
  60. </head>
  61. <body>
  62. <table id="list"></table>
  63. <div id="pager"></div>
  64. @*<div>
  65. <%= Html.Trirand().JQGrid("", "EditDialogGrid") %>
  66. </div>*@
  67. </body>
  68. </html>
Code for AdminController: After putting this URL http://localhost:26158/Admin into a browser, it calls the Index Action that returns the AddTopics View and does other operations.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using QuickBook.Models;
  8. namespace QuickBook.Controllers
  9. {
  10. public class AdminController : Controller
  11. {
  12. // GET: /Admin/
  13. EmployeeEntities edb = new EmployeeEntities();
  14. public ActionResult Index()
  15. {
  16. return View("AddTopics");
  17. }
  18. [HttpGet]
  19. public JsonResult GetTopics()
  20. {
  21. EmployeeEntities edb = new EmployeeEntities();
  22. var jsonData = new
  23. {
  24. total = 1,
  25. page = 1,
  26. records = edb.tblAddTopics.ToList().Count,
  27. rows = (
  28. from emp in edb.tblAddTopics.ToList()
  29. select new
  30. {
  31. id = emp.id,
  32. cell = new string[] {
  33. emp.id.ToString(), emp.topic_name.ToString(), emp.created_by.ToString(),emp.created_on.ToString()
  34. }
  35. }).ToArray()
  36. };
  37. return Json(jsonData, JsonRequestBehavior.AllowGet);
  38. }
  39. [HttpPost]
  40. public void EditTopics(tblAddTopic postData, string ClickEventId)
  41. {
  42. // EmployeeEntities edb = new EmployeeEntities();
  43. if (ClickEventId == "edit_list")
  44. {
  45. var editRecord =
  46. edb.tblAddTopics.Where(x => x.id == postData.id).SingleOrDefault();
  47. editRecord.topic_name = postData.topic_name;
  48. editRecord.created_by = postData.created_by;
  49. editRecord.created_on = postData.created_on;
  50. edb.SaveChanges();
  51. }
  52. else if (ClickEventId == null && postData.topic_name!=null)
  53. {
  54. //var editRecord =
  55. // edb.tblAddTopics.Where(x => x.id == postData.id).SingleOrDefault();
  56. //editRecord.topic_name = postData.topic_name;
  57. //editRecord.created_by = postData.created_by;
  58. //editRecord.created_on = postData.created_on;
  59. edb.tblAddTopics.Add(postData);
  60. edb.SaveChanges();
  61. }
  62. else if (postData.topic_name==null)
  63. {
  64. var DeleteRecord =
  65. edb.tblAddTopics.Where(x => x.id == postData.id).SingleOrDefault();
  66. edb.tblAddTopics.Remove(DeleteRecord);
  67. edb.SaveChanges();
  68. }
  69. }
  70. [HttpGet]
  71. public JsonResult GetQuestionNo()
  72. {
  73. EmployeeEntities objEmpRegistration = new EmployeeEntities();
  74. List<Question> Questions = new List<Question>();
  75. Questions = (from p in objEmpRegistration.tblAddQuestions.ToList()
  76. group p by p.category_id into g
  77. select new Question
  78. {
  79. QuestionNO = g.Count() + 1,
  80. categoryID = g.Key
  81. }).ToList();
  82. return Json(Questions,JsonRequestBehavior.AllowGet);
  83. }
  84. public ActionResult AddQuestion()
  85. {
  86. return View(new UpdateCategory());
  87. }
  88. [HttpPost]
  89. public void Savequestion(int dropdownid, string Ques,string Ans)
  90. {
  91. tblAddQuestion addobj = new tblAddQuestion();
  92. addobj.category_id = dropdownid;
  93. addobj.question = Ques;
  94. addobj.answer = Ans;
  95. addobj.created_by = 100;
  96. addobj.created_on = System.DateTime.Now;
  97. edb.tblAddQuestions.Add(addobj);
  98. edb.SaveChanges();
  99. // return ("successfully");
  100. }
  101. }
  102. }
Validation keywords

Kindly refer to the following image to understand grid-level validation.



Note: You can control more textual information at the grid.locale-en.js level, like caption, Add caption, the Submit button text and much more as shown in the image below.



I have added AddTopics.cshtml and Admincontroller.cs as a sample though I have already shared a database backup file in Part 1.

Integration of jqGrid With MVC4 Application: Part 1

Thanks.

Stay happy and stay coding.