Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 480.7k

How to filter out search using server side processing?

Feb 12 2020 3:52 AM
Hi Team
 
I have logic that does not filter out my search well, i am using dataTable and its using server side processing to retrieve data to my table from the database. I do have few records with data, but they dont filter at all. Please view my logic and help produce results better, thanks.
  1. // Controller class  
  2.   
  3.  [HttpPost]  
  4.        public ActionResult GetList()  
  5.         {  
  6.             //Server side Parameter.  
  7.             int start = Convert.ToInt32(Request["start"]);  
  8.             int length = Convert.ToInt32(Request["length"]);  
  9.             string searchValue = Request["search[value]"];  
  10.             string sortColumnName = Request["columns[" + Request["order[0][column]"] + "][name]"];  
  11.             string sortDirection = Request["order[0][dir]"];  
  12.   
  13.             List<TblEventsManagements> empList = new List<TblEventsManagements>();  
  14.             using (eNtsaOnlineRegistrationDBContext db = new eNtsaOnlineRegistrationDBContext())  
  15.             {  
  16.                 empList = db.TblEventsManagements.ToList<TblEventsManagements>();  
  17.                 int totalrows = empList.Count();  
  18.   
  19.   
  20.                 if (!string.IsNullOrEmpty(searchValue))  
  21.                 {  
  22.                     empList = empList.Where(x => x.TrainingType.ToLower().Contains(searchValue.ToLower()) || x.TrainingDescription.ToLower().Contains(searchValue.ToLower()) || x.Price.ToString().Contains(searchValue.ToLower())  
  23.                     || x.Venue.ToLower().Contains(searchValue.ToLower()) || x.Facilitator.ToLower().Contains(searchValue.ToLower()) || x.WhoAttend.ToLower().Contains(searchValue.ToLower()) || x.Rsvp.ToLower().Contains(searchValue.ToLower())).ToList<TblEventsManagements>();  
  24.   
  25.   
  26.                 }  
  27.                 int totalrowsafterfiltering = empList.Count;  
  28.   
  29.                 empList = empList.OrderBy(sortColumnName + " " + sortDirection).ToList<TblEventsManagements>();  
  30.                 empList = empList.Skip(start).Take(length).ToList<TblEventsManagements>();  
  31.   
  32.   
  33.                 return Json(new { data = empList, draw = Request["draw"], recordsTotal = totalrows, recordsFiltered = totalrowsafterfiltering }, JsonRequestBehavior.AllowGet);  
  34.             }  
  35.   
  36.   
  37.                   
  38.         }  
  39.   
  40.           
  41.   
  42. // View cshtml  
  43.   
  44.   
  45. @{  
  46.     ViewBag.Title = "EventManagement List";  
  47. }  
  48. <hr />  
  49. <hr />  
  50. <hr />  
  51. <h2>EventManagement List</h2>  
  52. <table id="EventManagementTable" class="ui celled table" style="width:100%">  
  53.     @*Semantic UI*@  
  54.     @*<table id="EventManagementTable" class="ui celled table">*@  
  55.     @*Bootstrap*@  
  56.     @*<table id="EventManagementTable" class="table table-striped table-bordered">*@  
  57.   
  58.     <thead>  
  59.         <tr>  
  60.   
  61.             <th>TrainingType</th>  
  62.             <th>TrainingDescription</th>  
  63.             <th>Price</th>  
  64.             <th>Venue</th>  
  65.             <th>Facilitator</th>  
  66.             <th>WhoAttend</th>  
  67.             <th>RSVP</th>  
  68.         </tr>  
  69.     </thead>  
  70.     <tfoot>  
  71.         <tr>  
  72.   
  73.             <th>TrainingType</th>  
  74.             <th>TrainingDescription</th>  
  75.             <th>Price</th>  
  76.             <th>Venue</th>  
  77.             <th>Facilitator</th>  
  78.             <th>WhoAttend</th>  
  79.             <th>RSVP</th>  
  80.         </tr>  
  81.     </tfoot>  
  82.   
  83. </table>  
  84.   
  85.   
  86. <link href="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.6/semantic.min.css" rel="stylesheet" />  
  87. <link href="https://cdn.datatables.net/1.10.15/css/dataTables.semanticui.min.css" rel="stylesheet" />  
  88.   
  89. @section scripts{  
  90.   
  91.       <script src="//cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>  
  92. <script src="https://cdn.datatables.net/1.10.15/js/dataTables.semanticui.min.js"></script>  
  93. <script src="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.6/semantic.min.js"></script>  
  94.   
  95.     <script>  
  96.   
  97.         $(document).ready(function () {  
  98.   
  99.                $("#EventManagementTable").DataTable({  
  100.                 "ajax": {  
  101.                     "url""/Dashboard/GetList",  
  102.                     "type""POST",  
  103.                     "datatype":"json"  
  104.                 },  
  105.                    "columns": [  
  106.                     {"data""TrainingType""name""TrainingType"},  
  107.                     { "data""TrainingDescription""name""TrainingDescription" },  
  108.                     { "data""Price""name""Price" },  
  109.                     { "data""Venue""name""Venue" },  
  110.                     { "data""Facilitator""name""Facilitator" },  
  111.                     { "data""WhoAttend""name""WhoAttend" },  
  112.                     {"data""RSVP""name""RSVP"},  
  113.                 ],  
  114.                   
  115.                 "serverSide""true",  
  116.                 "order":[0,"asc"],  
  117.                 "processing""true",  
  118.                 "language": {  
  119.                     "processing":"processing... please wait"  
  120.                 }  
  121.                   
  122.                   
  123.   
  124.             });  
  125.   
  126.         });  
  127.   
  128.   
  129.     </script>  
  130.   
  131.   
  132. }  
 

Answers (1)