In this article, we will create an ASP.NET MVC 4 Application, using AngularJS in Custom directive with multi check box. Previously, I created ASP.NET MVC 4 Application, using AngularJS, perform CRUD operations and Multi-Row Insert, using Web API. Let’s see.

Introduction

The link of my previous article is given below.

In this Angularcontroller folder in StudentViewcontroller.js, we will add custom directive for the child table information.

StudentViewcontroller.js (it is angular controller)

In JS file, we defined the custom directive.

  1. app.controller("StudentViewcontroller", function ($scope, RohitService, $location, ShareData, $window) {
  2. getdate();
  3. $scope.selectedRow = null; // initialize our variable to null
  4. function getdate() {
  5. var promisePost = RohitService.ShowStudent();
  6. promisePost.then(function (pl) {
  7. var stud = pl.data;
  8. $scope.student = stud;
  9. })
  10. }
  11. $scope.editEmployee = function (KeyId) {
  12. ShareData.KeyId = KeyId;
  13. var earl = "/studentRecord";
  14. $location.path(earl);
  15. }
  16. $scope.deleteEmployee = function (KeyId) {
  17. if ($window.confirm('Are you sure to delete ?')) {
  18. var promisePost = RohitService.deleteStudentKey(KeyId);
  19. promisePost.then(function (pl) {
  20. var stud = pl.data;
  21. alert('Your Record Deleted Successfully !');
  22. getdate();
  23. })
  24. }
  25. }
  26. $scope.selectEntity = function () {
  27. // If any entity is not checked, then uncheck the "allItemsSelected" checkbox
  28. for (var i = 0; i < $scope.student.length; i++) {
  29. if (!$scope.student[i].isChecked) {
  30. $scope.allItemsSelected = false;
  31. return;
  32. }
  33. }
  34. //If not the check the "allItemsSelected" checkbox
  35. $scope.allItemsSelected = true;
  36. };
  37. // This executes when checkbox in table header is checked
  38. $scope.selectAll = function () {
  39. for (var i = 0; i < $scope.student.length; i++) {
  40. $scope.student[i].isChecked = $scope.allItemsSelected;
  41. }
  42. };
  43. });
  44. //my custome directive for child table data infomation
  45. app.directive("myGetDirective", function (RohitService) {
  46. return {
  47. restrict: 'E',
  48. scope: {
  49. datasource: '='
  50. },
  51. link: function ($scope) {
  52. var index = $scope.$parent.$index;
  53. var id = $scope.$parent.student[index].KeyId;
  54. var promiseStudentRecord = RohitService.showEduStudent(id);
  55. promiseStudentRecord.then(function (pl) {
  56. var stud = pl.data;
  57. $scope.ClassModel = stud;
  58. },
  59. function (errorPl) {
  60. $scope.error = errorPl;
  61. });
  62. },
  63. templateUrl: "Home/View1"
  64. }
  65. });
View1.cshtml (it is View Folder)
  1. <div>
  2. <table>
  3. <tr>
  4. <td>
  5. Class
  6. </td>
  7. <td>
  8. Board
  9. </td>
  10. <td>
  11. Roll No.
  12. </td>
  13. </tr>
  14. <tr data-ng-repeat="cls in ClassModel">
  15. <td>
  16. {{cls.ClassName}}
  17. </td>
  18. <td>
  19. {{cls.BoardName}}
  20. </td>
  21. <td>
  22. {{cls.CRollNo}}
  23. </td>
  24. </tr>
  25. </table>
  26. </div>
StudentView.cshtml (it is View Folder)

In this view, we will add custom directive myGetDirective.
  1. @{
  2. ViewBag.Title = "Student View";
  3. }
  4. <h2>Student View</h2>
  5. <table id="testrohit">
  6. <tr>
  7. <thead>
  8. <td>
  9. <table>
  10. <tr>
  11. <th>
  12. <input type="checkbox" data-ng-model="allItemsSelected" data-ng-change="selectAll()">
  13. </th>
  14. <th>Student Name
  15. </th>
  16. <th>Father's Name
  17. </th>
  18. <th>Gender
  19. </th>
  20. <th>Mobile No.
  21. </th>
  22. <th>Action
  23. </th>
  24. </tr>
  25. </table>
  26. </td>
  27. </thead>
  28. </tr>
  29. <tr>
  30. <tbody data-ng-repeat="students in student" >
  31. <td>
  32. <table class="inner-table">
  33. <tr>
  34. <td width="10%">
  35. <input type="checkbox" data-ng-model="students.isChecked" data-ng-change="selectEntity()">
  36. </td>
  37. <td width="20%">{{students.SName | uppercase}}
  38. </td>
  39. <td width="20%">{{students.FName}}
  40. </td>
  41. <td width="20%">{{students.Gender}}
  42. </td>
  43. <td width="20%">{{students.MobileNo}}
  44. </td>
  45. <td width="10%">
  46. <span data-ng-click="toggleModal(students.KeyId)">View</span>
  47. <span data-ng-click="editEmployee(students.KeyId)">Edit</span>
  48. <span data-ng-click="deleteEmployee(students.KeyId)">Delete</span>
  49. </td>
  50. </tr>
  51. <tr>
  52. <td colspan="6">
  53. <div data-ng-if="students.isChecked">
  54. <my:get-directive datasource="ClassModel"></my:get-directive>
  55. </div>
  56. </td>
  57. </tr>
  58. </table>
  59. </td>
  60. </tbody>
  61. </tr>
  62. </table>
Now, update angularcontroller in js file, the view in studentview.cshtml and add view1.cshtml file, build your project and run it.

After running your Application, the output is given below.