5.9. Views

Since we need only the View/Customer/Index.cshtml view out of the five created for the Customer controller, you can delete the others from the folder.

You can see that the entire view consists of the header, the jqg table and the jqg-pager block for displaying the navigation bar. The rest is occupied by the script for initiating the grid, the navigation bar and the dialog box for editing records.

  1. @{
  2. ViewBag.Title = "Index";
  3. }
  4. <h2>Customers</h2>
  5. <table id="jqg"></table>
  6. <div id="jqg-pager"></div>
  7. <script type="text/javascript">
  8. $(document).ready(function () {
  9. var dbGrid = $("#jqg").jqGrid({
  10. url: '@Url.Action("GetData")', // URL to retrieve data
  11. datatype: "json", // data format
  12. mtype: "GET", // http type request
  13. // model description
  14. colModel: [
  15. {
  16. label: 'Id',
  17. name: 'CUSTOMER_ID', // field name
  18. key: true,
  19. hidden: true
  20. },
  21. {
  22. label: 'Name',
  23. name: 'NAME',
  24. width: 250,
  25. sortable: true,
  26. editable: true,
  27. edittype: "text", // field type in the editor
  28. search: true,
  29. searchoptions: {
  30. sopt: ['eq', 'bw', 'cn'] // allowed search operators
  31. },
  32. // size and maximum length for the input field
  33. editoptions: { size: 30, maxlength: 60 },
  34. // mandatory field
  35. editrules: { required: true }
  36. },
  37. {
  38. label: 'Address',
  39. name: 'ADDRESS',
  40. width: 300,
  41. sortable: false, // prohibit sorting
  42. editable: true,
  43. search: false, // prohibit searching
  44. edittype: "textarea",
  45. editoptions: { maxlength: 250, cols: 30, rows: 4 }
  46. },
  47. {
  48. label: 'Zip Code',
  49. name: 'ZIPCODE',
  50. width: 30,
  51. sortable: false,
  52. editable: true,
  53. search: false,
  54. edittype: "text",
  55. editoptions: { size: 30, maxlength: 10 },
  56. },
  57. {
  58. label: 'Phone',
  59. name: 'PHONE',
  60. width: 80,
  61. sortable: false,
  62. editable: true,
  63. search: false,
  64. edittype: "text",
  65. editoptions: { size: 30, maxlength: 14 },
  66. }
  67. ],
  68. rowNum: 500, // number of rows displayed
  69. loadonce: false, // load only once
  70. sortname: 'NAME', // sort by default by NAME column
  71. sortorder: "asc",
  72. width: window.innerWidth - 80, // grid width
  73. height: 500, // grid height
  74. viewrecords: true, // display the number of records
  75. caption: "Customers",
  76. pager: 'jqg-pager' // navigation item id
  77. });
  78. dbGrid.jqGrid('navGrid', '#jqg-pager', {
  79. search: true,
  80. add: true,
  81. edit: true,
  82. del: true,
  83. view: true,
  84. refresh: true,
  85. // button labels
  86. searchtext: "Find",
  87. addtext: "Add",
  88. edittext: "Edit",
  89. deltext: "Delete",
  90. viewtext: "View",
  91. viewtitle: "Selected record",
  92. refreshtext: "Refresh"
  93. },
  94. update("edit"),
  95. update("add"),
  96. update("del")
  97. );
  98. // function that returns the settings of the editor
  99. function update(act) {
  100. return {
  101. closeAfterAdd: true,
  102. closeAfterEdit: true,
  103. width: 400, // editor width
  104. reloadAfterSubmit: true,
  105. drag: true,
  106. // handler for sending the form of editing / deleting / adding
  107. onclickSubmit: function (params, postdata) {
  108. // get row id
  109. var selectedRow = dbGrid.getGridParam("selrow");
  110. // set URL depending on the operation
  111. switch (act) {
  112. case "add":
  113. params.url = '@Url.Action("Create")';
  114. break;
  115. case "edit":
  116. params.url = '@Url.Action("Edit")';
  117. postdata.CUSTOMER_ID = selectedRow;
  118. break;
  119. case "del":
  120. params.url = '@Url.Action("Delete")';
  121. postdata.CUSTOMER_ID = selectedRow;
  122. break;
  123. }
  124. },
  125. // processing results of sending forms (operations)
  126. afterSubmit: function (response, postdata) {
  127. var responseData = response.responseJSON;
  128. // check the result for error messages
  129. if (responseData.hasOwnProperty("error")) {
  130. if (responseData.error.length) {
  131. return [false, responseData.error];
  132. }
  133. }
  134. else {
  135. // refresh grid
  136. $(this).jqGrid(
  137. 'setGridParam',
  138. {
  139. datatype: 'json'
  140. }
  141. ).trigger('reloadGrid');
  142. }
  143. return [true, "", 0];
  144. }
  145. };
  146. };
  147. });
  148. </script>

It is important to configure the model properties correctly in order to display the grid properly, position input items on the edit form, configure validation for input forms and configure the sorting and search options. This configuration is not simple and has a lot of parameters. In the comments I have tried to describe the parameters being used. The full description of the model parameters can be found in the documentation for the jqGrid library in the ColModel API section.

Note that jqGrid does not automatically add hidden grid columns to the input form, though I think it would make sense at least for key fields. Consequently, we have to add the customer identifier to the request parameters for editing and deleting:

  1. case "edit":
  2. params.url = '@Url.Action("Edit")';
  3. postdata.CUSTOMER_ID = selectedRow;
  4. break;
  5. case "del":
  6. params.url = '@Url.Action("Delete")';
  7. postdata.CUSTOMER_ID = selectedRow;
  8. break;

The working page with the list of customers will look like this:

fbdevgd30 mvc 011 en

Figure 39. Customer list view

fbdevgd30 mvc 012 en

Figure 40. A customer selected for editing

The controller and view for the product UI are implemented in a similar way. We will not describe them here in detail. You can either write them yourself or use the source code linked at the end of this chapter.