Advertisement
Guest User

jQuery view

a guest
Jun 16th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 KB | None | 0 0
  1. @model FullCRUDImplementationWithJquery.Models.StudentViewModel
  2. @{
  3. Layout = null;
  4. }
  5. <script src="~/Scripts/jquery-3.3.1.min.js"></script>
  6. <script src="~/Scripts/bootstrap.min.js"></script>
  7. <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
  8.  
  9. <div class="container" style="margin-top:3%">
  10. <a href="#" class="btn btn-info" onclick="AddNewStudent(0)">Add New Student</a> <br /><br />
  11.  
  12. <div class="container">
  13. <b>Search By : </b>
  14. <select id="SearchBy">
  15. <option value="StudentID">Student ID</option>
  16. <option value="StudentName">Student Name</option>
  17. <option value="Email">Email</option>
  18. <option value="Department">Department</option>
  19. </select> <br /><br />
  20. @Html.TextBox("Search")<input type="submit" id="SearchBtn" value="Search" /> <br /><br />
  21. </div>
  22.  
  23.  
  24. <table class="table table-striped">
  25. <thead>
  26. <tr>
  27. <th>Student ID</th>
  28. <th>Student Name</th>
  29. <th>Email</th>
  30. <th>Department</th>
  31. <th>Action(Edit)</th>
  32. <th>Action(Delete)</th>
  33. </tr>
  34. </thead>
  35. <tbody id="SetStudentList">
  36. <tr id="LoadingStatus" style="color:red"></tr>
  37. </tbody>
  38. </table>
  39.  
  40. @*Create a Popup Modal With Registration Form For Add Or Edit Student Record*@
  41.  
  42. <div class="modal fade" id="MyModal">
  43. <div class="modal-dialog">
  44. <div class="modal-content">
  45. <div class="modal-header">
  46. <a href="#" class="close" data-dismiss="modal">&times;</a>
  47. <h4 id="ModalTitle"></h4>
  48. </div>
  49. <div class="modal-body">
  50. <form id="form">
  51. <fieldset id="SubmitForm">
  52. @Html.HiddenFor(m => m.StudentId, new { @id = "StudId" })
  53. <div class="form-group">
  54. @Html.TextBoxFor(m => m.StudentName, new { @id = "StudName", @class = "form-control", @placeholder = "Name" })
  55. </div>
  56. <div class="form-group">
  57. @Html.TextBoxFor(m => m.Email, new { @id = "Email", @class = "form-control", @placeholder = "Email" })
  58. </div>
  59. <div class="form-group">
  60. @Html.DropDownListFor(m => m.DepartmentId, ViewBag.ListOfDepartment as SelectList, "--Select Dept--", new { @id = "DropDwn", @class = "form-control" })
  61. </div>
  62. <div class="form-group">
  63. <a href="#" class="btn btn-block btn-danger" id="SaveStudentRecord">Save</a>
  64. </div>
  65. </fieldset>
  66. </form>
  67. </div>
  68. </div>
  69. </div>
  70. </div>
  71.  
  72.  
  73. @*Create a PopUp Modal For DeleteConfirmation*@
  74.  
  75. <div class="modal fade" id="DeleteConfirmation">
  76. <div class="modal-dialog">
  77. <div class="modal-content">
  78. <div class="modal-header">
  79. <a href="#" class="close" data-dismiss="modal">&times;</a>
  80. <h4>Delete Student Record</h4>
  81. </div>
  82. <div class="modal-body">
  83. <h4>Are You Sure? You Want To Delete This Record.</h4>
  84. </div>
  85. <div class="modal-footer">
  86. <a href="#" class="btn btn-primary" data-dismiss="modal">Cancel</a>
  87. <a href="#" class="btn btn-danger" onclick="ConfirmDelete()">Confirm</a>
  88. </div>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93.  
  94. <script>
  95. $("#LoadingStatus").html("Loading.... ");
  96. $.get("/Home/GetStudentList", null, DataBind);
  97. function DataBind(StudentList) {
  98. var SetData = $("#SetStudentList");
  99. for (var i = 0; i<StudentList.length; i++) {
  100. var Data = "<tr class='row_" + StudentList[i].StudentId + "'>" +
  101. "<td>" + StudentList[i].StudentId + "</td>" +
  102. "<td>" + StudentList[i].StudentName + "</td>" +
  103. "<td>" + StudentList[i].Email + "</td>" +
  104. "<td>" + StudentList[i].DepartmentName + "</td>" +
  105. "<td>" + "<a href='#' class='btn btn-warning' onclick='EditStudentRecord(" + StudentList[i].StudentId + ")'><span class='glyphicon glyphicon-edit'></span></a>" + "</td>" +
  106. "<td>" + "<a href='#' class='btn btn-danger' onclick='DeleteStudentRecord(" + StudentList[i].StudentId + ")'><span class='glyphicon glyphicon-trash'></span></a>" + "</td>" +
  107. "</tr>";
  108. SetData.append(Data);
  109. $("#LoadingStatus").html(" ");
  110. }
  111. }
  112.  
  113. //Show The Popup Modal For Add New Student
  114.  
  115. function AddNewStudent(StudentId) {
  116. $("#form")[0].reset();
  117. $("#StudID").val(0);
  118. $("#DropDwn option:selected").text("--Select Dept--");
  119. $("#ModalTitle").html("Add New Student");
  120. $("#MyModal").modal();
  121. }
  122.  
  123. //Show The Popup Modal For Edit Student Record
  124.  
  125. function EditStudentRecord(StudentId) {
  126. var url = "/Home/GetStudentById?StudentId=" + StudentId;
  127. $("#ModalTitle").html("Update Student Record");
  128. $("#MyModal").modal();
  129. $.ajax({
  130. type: "GET",
  131. url: url,
  132. success: function (data) {
  133. var obj = JSON.parse(data);
  134. $("#StudID").val(obj.StudentId);
  135. $("#StudName").val(obj.StudentName);
  136. $("#Email").val(obj.Email);
  137. $("#DropDwn option:selected").text(obj.tblDepartment.DepartmentName);
  138. $("#DropDwn option:selected").val(obj.DepartmentId);
  139. }
  140. })
  141. }
  142.  
  143. $("#SaveStudentRecord").click(function () {
  144. var data = $("#SubmitForm").serialize();
  145. $.ajax({
  146. type: "Post",
  147. url: "/Home/SaveDataInDatabase",
  148. data: data,
  149. success: function (result) {
  150. alert("Success!..");
  151. window.location.href = "/Home/index";
  152. $("#MyModal").modal("hide");
  153. }
  154. })
  155. })
  156.  
  157. //Show The Popup Modal For DeleteConfirmation
  158.  
  159. function DeleteStudentRecord(StudentId) {
  160. $("#StudId").val(StudentId);
  161. $("#DeleteConfirmation").modal("show");
  162. }
  163. function ConfirmDelete() {
  164. var StudId = $("#StudId").val();
  165. $.ajax({
  166. type: "POST",
  167. url: "/Home/DeleteStudentRecord?StudentId=" + StudId,
  168. success: function (result) {
  169. $("#DeleteConfirmation").modal("hide");
  170. $(".row_" + StudId).remove();
  171. }
  172. })
  173. }
  174.  
  175. $(document).read(function () {
  176. $("#SearchBtn").click(function () {
  177.  
  178. var SearchBy = $("#SearchBy").val();
  179. var SearchValue = $("#Search").val();
  180. var SetData = $("#SetStudentList");
  181. SetData.html("");
  182. $.ajax({
  183. type: "post",
  184. url: "/Home/GetSearchingData?SearchBy=" + SearchBy + "&SearchValue=" + SearchValue,
  185. contentType: "html",
  186. success: function (result) {
  187. if (result.length == 0) {
  188. SetData.append('<tr style="color:red"><td colspan="3">No Match Data</td></tr>')
  189. }
  190. else {
  191. $.each(result, function (index, value) {
  192. var Data = "<tr class='row_" + StudentList[i].StudentId + "'>" +
  193. "<td>" + StudentList[i].StudentId + "</td>" +
  194. "<td>" + StudentList[i].StudentName + "</td>" +
  195. "<td>" + StudentList[i].Email + "</td>" +
  196. "<td>" + StudentList[i].DepartmentName + "</td>" +
  197. "<td>" + "<a href='#' class='btn btn-warning' onclick='EditStudentRecord(" + StudentList[i].StudentId + ")'><span class='glyphicon glyphicon-edit'></span></a>" + "</td>" +
  198. "<td>" + "<a href='#' class='btn btn-danger' onclick='DeleteStudentRecord(" + StudentList[i].StudentId + ")'><span class='glyphicon glyphicon-trash'></span></a>" + "</td>" +
  199. "</tr>";
  200. SetData.append(Data);
  201.  
  202. });
  203. }
  204. }
  205.  
  206. });
  207. });
  208.  
  209. });
  210. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement