TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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.
// Controller class
[HttpPost]
public
ActionResult GetList()
{
//Server side Parameter.
int
start = Convert.ToInt32(Request[
"start"
]);
int
length = Convert.ToInt32(Request[
"length"
]);
string searchValue = Request[
"search[value]"
];
string sortColumnName = Request[
"columns["
+ Request[
"order[0][column]"
] +
"][name]"
];
string sortDirection = Request[
"order[0][dir]"
];
List<TblEventsManagements> empList =
new
List<TblEventsManagements>();
using
(eNtsaOnlineRegistrationDBContext db =
new
eNtsaOnlineRegistrationDBContext())
{
empList = db.TblEventsManagements.ToList<TblEventsManagements>();
int
totalrows = empList.Count();
if
(!string.IsNullOrEmpty(searchValue))
{
empList = empList.Where(x => x.TrainingType.ToLower().Contains(searchValue.ToLower()) || x.TrainingDescription.ToLower().Contains(searchValue.ToLower()) || x.Price.ToString().Contains(searchValue.ToLower())
|| 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>();
}
int
totalrowsafterfiltering = empList.Count;
empList = empList.OrderBy(sortColumnName +
" "
+ sortDirection).ToList<TblEventsManagements>();
empList = empList.Skip(start).Take(length).ToList<TblEventsManagements>();
return
Json(
new
{ data = empList, draw = Request[
"draw"
], recordsTotal = totalrows, recordsFiltered = totalrowsafterfiltering }, JsonRequestBehavior.AllowGet);
}
}
// View cshtml
@{
ViewBag.Title =
"EventManagement List"
;
}
<hr />
<hr />
<hr />
<h2>EventManagement List</h2>
<table id=
"EventManagementTable"
class
=
"ui celled table"
style=
"width:100%"
>
@*Semantic UI*@
@*<table id=
"EventManagementTable"
class
=
"ui celled table"
>*@
@*Bootstrap*@
@*<table id=
"EventManagementTable"
class
=
"table table-striped table-bordered"
>*@
<thead>
<tr>
<th>TrainingType</th>
<th>TrainingDescription</th>
<th>Price</th>
<th>Venue</th>
<th>Facilitator</th>
<th>WhoAttend</th>
<th>RSVP</th>
</tr>
</thead>
<tfoot>
<tr>
<th>TrainingType</th>
<th>TrainingDescription</th>
<th>Price</th>
<th>Venue</th>
<th>Facilitator</th>
<th>WhoAttend</th>
<th>RSVP</th>
</tr>
</tfoot>
</table>
<link href=
"//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.6/semantic.min.css"
rel=
"stylesheet"
/>
<link href=
"https://cdn.datatables.net/1.10.15/css/dataTables.semanticui.min.css"
rel=
"stylesheet"
/>
@section scripts{
<script src=
"//cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"
></script>
<script src=
"https://cdn.datatables.net/1.10.15/js/dataTables.semanticui.min.js"
></script>
<script src=
"//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.6/semantic.min.js"
></script>
<script>
$(document).ready(function () {
$(
"#EventManagementTable"
).DataTable({
"ajax"
: {
"url"
:
"/Dashboard/GetList"
,
"type"
:
"POST"
,
"datatype"
:
"json"
},
"columns"
: [
{
"data"
:
"TrainingType"
,
"name"
:
"TrainingType"
},
{
"data"
:
"TrainingDescription"
,
"name"
:
"TrainingDescription"
},
{
"data"
:
"Price"
,
"name"
:
"Price"
},
{
"data"
:
"Venue"
,
"name"
:
"Venue"
},
{
"data"
:
"Facilitator"
,
"name"
:
"Facilitator"
},
{
"data"
:
"WhoAttend"
,
"name"
:
"WhoAttend"
},
{
"data"
:
"RSVP"
,
"name"
:
"RSVP"
},
],
"serverSide"
:
"true"
,
"order"
:[0,
"asc"
],
"processing"
:
"true"
,
"language"
: {
"processing"
:
"processing... please wait"
}
});
});
</script>
}
Reply
Answers (
1
)
About VS 2019 templates
empty record list on your Jquery dataTable