Jquery dataTables serverSide in ASP MVC
I tried this but i still need to fix my controller i think
I need to get the data from my database to the datatable
this is my table and my js
<table id="myTable" class="display nowrap" style="width:100%">
<thead>
<tr>
<th>Status</th>
<th>Date and Time</th>
<th>Comments</th>
</tr>
</thead>
</table>
my Js
<!--Import jQuery before export.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<!--Data Table-->
<script type="text/javascript" src=" https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src=" https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>
<!--Export table buttons-->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.4/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.print.min.js"></script>
<!--Export table button CSS-->
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.2.4/css/buttons.dataTables.min.css">
<script type="text/javascript">
$(document).ready(function () {
$.noConflict();
$('#myTable').DataTable({
"ajax": {
"url": "/Profile/Index/viewModel/EmployeeCollection/HistoryOfStatuses",
"type": "POST",
"datatype": "json"
},
"columns": [
{ "data": "CheckStatus", "name": "Status" },
{ "data": "CheckTime", "name": "Date and Time" }
],
"serverSide": "true",
"order": [0, "asc"],
dom: 'Bfrtip',
buttons: [
'copy', 'pdf', 'csv', 'print', 'excel'
]
});
});
My Controller looks like this In this datatable i need to use the HistoryOfStatuses
public IActionResult Index(int? Id)
{
using (var context = new RSAT.Api.Data.Proxy.ATT2018NOVUSContext())
{
var baseViewModel = base.GetLayoutViewModel();
var viewModel = new HomeViewModel()
{
User = baseViewModel.User,
RoleCollection = baseViewModel.RoleCollection,
TableCollection = baseViewModel.TableCollection,
//Olap = baseViewModel.Olap,
//Localization = baseViewModel.Localization,
EmployeeCollection = (from userinfo in context.Userinfo
where userinfo.Userid == Id
join department in context.Dept on userinfo.Deptid equals department.Deptid
select new Employee()
{
Id = userinfo.Userid,
Name = userinfo.Name,
Picture = userinfo.Picture,
Department = department.DeptName,
CardNumber = userinfo.CardNum,
Status = userinfo.UserFlag.ToString(),
ActualCheckinStatuse = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).FirstOrDefault(),
HistoryOfStatuses = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).ToList(),
TodayCheckedStatus = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
where checkinout.CheckTime.Date == DateTime.Today.Date
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).ToList()
}).ToList()
};
return View(viewModel);
}
And this is my class employee Controller that im going to use.
public class Employee : BaseEntity
{
public Employee()
{
this.HistoryOfStatuses = new List<Checkinout>();
this.TodayCheckedStatus = new List<Checkinout>();
}
public string Name { get; set; }
public string Department { get; set; }
public string CardNumber { get; set; }
public string Status { get; set; }
public byte Picture { get; set; }
public Checkinout ActualCheckinStatuse { get; set; }
public List<Checkinout> HistoryOfStatuses { get; set; }
public List<Checkinout> TodayCheckedStatus { get; set; }
public int UserId { get; internal set; }
public string UName { get; set; }
public string UField { get; set; }
public string UDeg { get; set; }
//test
}
I need to use the datatable to sort and to download data in pdf and exel.
c# jquery .net sql-server asp.net-mvc
add a comment |
I tried this but i still need to fix my controller i think
I need to get the data from my database to the datatable
this is my table and my js
<table id="myTable" class="display nowrap" style="width:100%">
<thead>
<tr>
<th>Status</th>
<th>Date and Time</th>
<th>Comments</th>
</tr>
</thead>
</table>
my Js
<!--Import jQuery before export.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<!--Data Table-->
<script type="text/javascript" src=" https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src=" https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>
<!--Export table buttons-->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.4/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.print.min.js"></script>
<!--Export table button CSS-->
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.2.4/css/buttons.dataTables.min.css">
<script type="text/javascript">
$(document).ready(function () {
$.noConflict();
$('#myTable').DataTable({
"ajax": {
"url": "/Profile/Index/viewModel/EmployeeCollection/HistoryOfStatuses",
"type": "POST",
"datatype": "json"
},
"columns": [
{ "data": "CheckStatus", "name": "Status" },
{ "data": "CheckTime", "name": "Date and Time" }
],
"serverSide": "true",
"order": [0, "asc"],
dom: 'Bfrtip',
buttons: [
'copy', 'pdf', 'csv', 'print', 'excel'
]
});
});
My Controller looks like this In this datatable i need to use the HistoryOfStatuses
public IActionResult Index(int? Id)
{
using (var context = new RSAT.Api.Data.Proxy.ATT2018NOVUSContext())
{
var baseViewModel = base.GetLayoutViewModel();
var viewModel = new HomeViewModel()
{
User = baseViewModel.User,
RoleCollection = baseViewModel.RoleCollection,
TableCollection = baseViewModel.TableCollection,
//Olap = baseViewModel.Olap,
//Localization = baseViewModel.Localization,
EmployeeCollection = (from userinfo in context.Userinfo
where userinfo.Userid == Id
join department in context.Dept on userinfo.Deptid equals department.Deptid
select new Employee()
{
Id = userinfo.Userid,
Name = userinfo.Name,
Picture = userinfo.Picture,
Department = department.DeptName,
CardNumber = userinfo.CardNum,
Status = userinfo.UserFlag.ToString(),
ActualCheckinStatuse = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).FirstOrDefault(),
HistoryOfStatuses = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).ToList(),
TodayCheckedStatus = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
where checkinout.CheckTime.Date == DateTime.Today.Date
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).ToList()
}).ToList()
};
return View(viewModel);
}
And this is my class employee Controller that im going to use.
public class Employee : BaseEntity
{
public Employee()
{
this.HistoryOfStatuses = new List<Checkinout>();
this.TodayCheckedStatus = new List<Checkinout>();
}
public string Name { get; set; }
public string Department { get; set; }
public string CardNumber { get; set; }
public string Status { get; set; }
public byte Picture { get; set; }
public Checkinout ActualCheckinStatuse { get; set; }
public List<Checkinout> HistoryOfStatuses { get; set; }
public List<Checkinout> TodayCheckedStatus { get; set; }
public int UserId { get; internal set; }
public string UName { get; set; }
public string UField { get; set; }
public string UDeg { get; set; }
//test
}
I need to use the datatable to sort and to download data in pdf and exel.
c# jquery .net sql-server asp.net-mvc
hi..are you sure to retunr View(viewModel) ? .. not a Jsonresult(viewModel) instead?
– federico scamuzzi
Nov 21 '18 at 12:46
Your AJAX URL seem to be wrong here. Also no need to return all instances of viewmodel, because you have only 2 columns in datatable.
– Tetsuya Yamamoto
Nov 22 '18 at 2:33
add a comment |
I tried this but i still need to fix my controller i think
I need to get the data from my database to the datatable
this is my table and my js
<table id="myTable" class="display nowrap" style="width:100%">
<thead>
<tr>
<th>Status</th>
<th>Date and Time</th>
<th>Comments</th>
</tr>
</thead>
</table>
my Js
<!--Import jQuery before export.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<!--Data Table-->
<script type="text/javascript" src=" https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src=" https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>
<!--Export table buttons-->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.4/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.print.min.js"></script>
<!--Export table button CSS-->
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.2.4/css/buttons.dataTables.min.css">
<script type="text/javascript">
$(document).ready(function () {
$.noConflict();
$('#myTable').DataTable({
"ajax": {
"url": "/Profile/Index/viewModel/EmployeeCollection/HistoryOfStatuses",
"type": "POST",
"datatype": "json"
},
"columns": [
{ "data": "CheckStatus", "name": "Status" },
{ "data": "CheckTime", "name": "Date and Time" }
],
"serverSide": "true",
"order": [0, "asc"],
dom: 'Bfrtip',
buttons: [
'copy', 'pdf', 'csv', 'print', 'excel'
]
});
});
My Controller looks like this In this datatable i need to use the HistoryOfStatuses
public IActionResult Index(int? Id)
{
using (var context = new RSAT.Api.Data.Proxy.ATT2018NOVUSContext())
{
var baseViewModel = base.GetLayoutViewModel();
var viewModel = new HomeViewModel()
{
User = baseViewModel.User,
RoleCollection = baseViewModel.RoleCollection,
TableCollection = baseViewModel.TableCollection,
//Olap = baseViewModel.Olap,
//Localization = baseViewModel.Localization,
EmployeeCollection = (from userinfo in context.Userinfo
where userinfo.Userid == Id
join department in context.Dept on userinfo.Deptid equals department.Deptid
select new Employee()
{
Id = userinfo.Userid,
Name = userinfo.Name,
Picture = userinfo.Picture,
Department = department.DeptName,
CardNumber = userinfo.CardNum,
Status = userinfo.UserFlag.ToString(),
ActualCheckinStatuse = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).FirstOrDefault(),
HistoryOfStatuses = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).ToList(),
TodayCheckedStatus = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
where checkinout.CheckTime.Date == DateTime.Today.Date
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).ToList()
}).ToList()
};
return View(viewModel);
}
And this is my class employee Controller that im going to use.
public class Employee : BaseEntity
{
public Employee()
{
this.HistoryOfStatuses = new List<Checkinout>();
this.TodayCheckedStatus = new List<Checkinout>();
}
public string Name { get; set; }
public string Department { get; set; }
public string CardNumber { get; set; }
public string Status { get; set; }
public byte Picture { get; set; }
public Checkinout ActualCheckinStatuse { get; set; }
public List<Checkinout> HistoryOfStatuses { get; set; }
public List<Checkinout> TodayCheckedStatus { get; set; }
public int UserId { get; internal set; }
public string UName { get; set; }
public string UField { get; set; }
public string UDeg { get; set; }
//test
}
I need to use the datatable to sort and to download data in pdf and exel.
c# jquery .net sql-server asp.net-mvc
I tried this but i still need to fix my controller i think
I need to get the data from my database to the datatable
this is my table and my js
<table id="myTable" class="display nowrap" style="width:100%">
<thead>
<tr>
<th>Status</th>
<th>Date and Time</th>
<th>Comments</th>
</tr>
</thead>
</table>
my Js
<!--Import jQuery before export.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<!--Data Table-->
<script type="text/javascript" src=" https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src=" https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>
<!--Export table buttons-->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.4/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.print.min.js"></script>
<!--Export table button CSS-->
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.2.4/css/buttons.dataTables.min.css">
<script type="text/javascript">
$(document).ready(function () {
$.noConflict();
$('#myTable').DataTable({
"ajax": {
"url": "/Profile/Index/viewModel/EmployeeCollection/HistoryOfStatuses",
"type": "POST",
"datatype": "json"
},
"columns": [
{ "data": "CheckStatus", "name": "Status" },
{ "data": "CheckTime", "name": "Date and Time" }
],
"serverSide": "true",
"order": [0, "asc"],
dom: 'Bfrtip',
buttons: [
'copy', 'pdf', 'csv', 'print', 'excel'
]
});
});
My Controller looks like this In this datatable i need to use the HistoryOfStatuses
public IActionResult Index(int? Id)
{
using (var context = new RSAT.Api.Data.Proxy.ATT2018NOVUSContext())
{
var baseViewModel = base.GetLayoutViewModel();
var viewModel = new HomeViewModel()
{
User = baseViewModel.User,
RoleCollection = baseViewModel.RoleCollection,
TableCollection = baseViewModel.TableCollection,
//Olap = baseViewModel.Olap,
//Localization = baseViewModel.Localization,
EmployeeCollection = (from userinfo in context.Userinfo
where userinfo.Userid == Id
join department in context.Dept on userinfo.Deptid equals department.Deptid
select new Employee()
{
Id = userinfo.Userid,
Name = userinfo.Name,
Picture = userinfo.Picture,
Department = department.DeptName,
CardNumber = userinfo.CardNum,
Status = userinfo.UserFlag.ToString(),
ActualCheckinStatuse = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).FirstOrDefault(),
HistoryOfStatuses = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).ToList(),
TodayCheckedStatus = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
where checkinout.CheckTime.Date == DateTime.Today.Date
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).ToList()
}).ToList()
};
return View(viewModel);
}
And this is my class employee Controller that im going to use.
public class Employee : BaseEntity
{
public Employee()
{
this.HistoryOfStatuses = new List<Checkinout>();
this.TodayCheckedStatus = new List<Checkinout>();
}
public string Name { get; set; }
public string Department { get; set; }
public string CardNumber { get; set; }
public string Status { get; set; }
public byte Picture { get; set; }
public Checkinout ActualCheckinStatuse { get; set; }
public List<Checkinout> HistoryOfStatuses { get; set; }
public List<Checkinout> TodayCheckedStatus { get; set; }
public int UserId { get; internal set; }
public string UName { get; set; }
public string UField { get; set; }
public string UDeg { get; set; }
//test
}
I need to use the datatable to sort and to download data in pdf and exel.
c# jquery .net sql-server asp.net-mvc
c# jquery .net sql-server asp.net-mvc
edited Nov 21 '18 at 12:42
E.Lahu
asked Nov 21 '18 at 12:16
E.LahuE.Lahu
248
248
hi..are you sure to retunr View(viewModel) ? .. not a Jsonresult(viewModel) instead?
– federico scamuzzi
Nov 21 '18 at 12:46
Your AJAX URL seem to be wrong here. Also no need to return all instances of viewmodel, because you have only 2 columns in datatable.
– Tetsuya Yamamoto
Nov 22 '18 at 2:33
add a comment |
hi..are you sure to retunr View(viewModel) ? .. not a Jsonresult(viewModel) instead?
– federico scamuzzi
Nov 21 '18 at 12:46
Your AJAX URL seem to be wrong here. Also no need to return all instances of viewmodel, because you have only 2 columns in datatable.
– Tetsuya Yamamoto
Nov 22 '18 at 2:33
hi..are you sure to retunr View(viewModel) ? .. not a Jsonresult(viewModel) instead?
– federico scamuzzi
Nov 21 '18 at 12:46
hi..are you sure to retunr View(viewModel) ? .. not a Jsonresult(viewModel) instead?
– federico scamuzzi
Nov 21 '18 at 12:46
Your AJAX URL seem to be wrong here. Also no need to return all instances of viewmodel, because you have only 2 columns in datatable.
– Tetsuya Yamamoto
Nov 22 '18 at 2:33
Your AJAX URL seem to be wrong here. Also no need to return all instances of viewmodel, because you have only 2 columns in datatable.
– Tetsuya Yamamoto
Nov 22 '18 at 2:33
add a comment |
2 Answers
2
active
oldest
votes
You are displaying status list in datatable, but you are returning the employee viewModel
, I think you need to check your code and return proper data. Like below code
return Json(new
{
draw = model.draw,
recordsTotal = viewModel.HistoryOfStatuses.Count(),
recordsFiltered = viewModel.HistoryOfStatuses.Count(),
data = viewModel.HistoryOfStatuses
}, JsonRequestBehavior.AllowGet);
You can check the link Sample implementation of serverside processing in C# MVC
add a comment |
This is how i fix it My Controller
[HttpPost]
public string GetData()
{
string sJSONResponse = "";
List<string> data = new List<string>();
using (var context = new RSAT.Api.Data.Proxy.ATT2018NOVUSContext())
{
var baseViewModel = (from userinfo in context.Userinfo
join department in context.Dept on userinfo.Deptid equals department.Deptid
select new
{
Id = userinfo.Userid,
Name = userinfo.Name,
Department = department.DeptName,
CardNumber = userinfo.CardNum,
Status = userinfo.UserFlag.ToString(),
HistoryOfStatuses = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).ToList(),
}).ToList();
foreach (var i in baseViewModel)
{
foreach (var h in i.HistoryOfStatuses)
{
var s = "["" + h.CheckStatus + ""," + """ + h.CheckTime + ""]";
data.Add(s);
}
}
string dataRez = "[";
foreach (var i in data)
{
dataRez += i + ",";
}
dataRez = dataRez.Remove(dataRez.Length - 1);
dataRez = dataRez + "]";
sJSONResponse = "{"draw": 1,"recordsTotal": 57,"recordsFiltered": 57,"data":" + dataRez + "}";
}
return sJSONResponse;
}
And my Script
<!--Import jQuery before export.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<!--Data Table-->
<script type="text/javascript" src=" https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src=" https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>
<!--Export table buttons-->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.4/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.print.min.js"></script>
<!--Export table button CSS-->
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.2.4/css/buttons.dataTables.min.css">
<script type="text/javascript">
$(document).ready(function () {
$.noConflict();
$('#myTable1').DataTable({
"ajax": {
"url": "/Profile/GetData",
"type": "POST",
"datatype": "application/json"
},
"serverSide": "true",
"order": [0, "asc"],
"processing": "true",
dom: 'Bfrtip',
buttons: [
'copy', 'pdf', 'csv', 'print', 'excel'
]
});
});
</script>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411853%2fjquery-datatables-serverside-in-asp-mvc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are displaying status list in datatable, but you are returning the employee viewModel
, I think you need to check your code and return proper data. Like below code
return Json(new
{
draw = model.draw,
recordsTotal = viewModel.HistoryOfStatuses.Count(),
recordsFiltered = viewModel.HistoryOfStatuses.Count(),
data = viewModel.HistoryOfStatuses
}, JsonRequestBehavior.AllowGet);
You can check the link Sample implementation of serverside processing in C# MVC
add a comment |
You are displaying status list in datatable, but you are returning the employee viewModel
, I think you need to check your code and return proper data. Like below code
return Json(new
{
draw = model.draw,
recordsTotal = viewModel.HistoryOfStatuses.Count(),
recordsFiltered = viewModel.HistoryOfStatuses.Count(),
data = viewModel.HistoryOfStatuses
}, JsonRequestBehavior.AllowGet);
You can check the link Sample implementation of serverside processing in C# MVC
add a comment |
You are displaying status list in datatable, but you are returning the employee viewModel
, I think you need to check your code and return proper data. Like below code
return Json(new
{
draw = model.draw,
recordsTotal = viewModel.HistoryOfStatuses.Count(),
recordsFiltered = viewModel.HistoryOfStatuses.Count(),
data = viewModel.HistoryOfStatuses
}, JsonRequestBehavior.AllowGet);
You can check the link Sample implementation of serverside processing in C# MVC
You are displaying status list in datatable, but you are returning the employee viewModel
, I think you need to check your code and return proper data. Like below code
return Json(new
{
draw = model.draw,
recordsTotal = viewModel.HistoryOfStatuses.Count(),
recordsFiltered = viewModel.HistoryOfStatuses.Count(),
data = viewModel.HistoryOfStatuses
}, JsonRequestBehavior.AllowGet);
You can check the link Sample implementation of serverside processing in C# MVC
answered Nov 21 '18 at 13:02
Aquib IqbalAquib Iqbal
21417
21417
add a comment |
add a comment |
This is how i fix it My Controller
[HttpPost]
public string GetData()
{
string sJSONResponse = "";
List<string> data = new List<string>();
using (var context = new RSAT.Api.Data.Proxy.ATT2018NOVUSContext())
{
var baseViewModel = (from userinfo in context.Userinfo
join department in context.Dept on userinfo.Deptid equals department.Deptid
select new
{
Id = userinfo.Userid,
Name = userinfo.Name,
Department = department.DeptName,
CardNumber = userinfo.CardNum,
Status = userinfo.UserFlag.ToString(),
HistoryOfStatuses = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).ToList(),
}).ToList();
foreach (var i in baseViewModel)
{
foreach (var h in i.HistoryOfStatuses)
{
var s = "["" + h.CheckStatus + ""," + """ + h.CheckTime + ""]";
data.Add(s);
}
}
string dataRez = "[";
foreach (var i in data)
{
dataRez += i + ",";
}
dataRez = dataRez.Remove(dataRez.Length - 1);
dataRez = dataRez + "]";
sJSONResponse = "{"draw": 1,"recordsTotal": 57,"recordsFiltered": 57,"data":" + dataRez + "}";
}
return sJSONResponse;
}
And my Script
<!--Import jQuery before export.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<!--Data Table-->
<script type="text/javascript" src=" https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src=" https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>
<!--Export table buttons-->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.4/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.print.min.js"></script>
<!--Export table button CSS-->
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.2.4/css/buttons.dataTables.min.css">
<script type="text/javascript">
$(document).ready(function () {
$.noConflict();
$('#myTable1').DataTable({
"ajax": {
"url": "/Profile/GetData",
"type": "POST",
"datatype": "application/json"
},
"serverSide": "true",
"order": [0, "asc"],
"processing": "true",
dom: 'Bfrtip',
buttons: [
'copy', 'pdf', 'csv', 'print', 'excel'
]
});
});
</script>
add a comment |
This is how i fix it My Controller
[HttpPost]
public string GetData()
{
string sJSONResponse = "";
List<string> data = new List<string>();
using (var context = new RSAT.Api.Data.Proxy.ATT2018NOVUSContext())
{
var baseViewModel = (from userinfo in context.Userinfo
join department in context.Dept on userinfo.Deptid equals department.Deptid
select new
{
Id = userinfo.Userid,
Name = userinfo.Name,
Department = department.DeptName,
CardNumber = userinfo.CardNum,
Status = userinfo.UserFlag.ToString(),
HistoryOfStatuses = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).ToList(),
}).ToList();
foreach (var i in baseViewModel)
{
foreach (var h in i.HistoryOfStatuses)
{
var s = "["" + h.CheckStatus + ""," + """ + h.CheckTime + ""]";
data.Add(s);
}
}
string dataRez = "[";
foreach (var i in data)
{
dataRez += i + ",";
}
dataRez = dataRez.Remove(dataRez.Length - 1);
dataRez = dataRez + "]";
sJSONResponse = "{"draw": 1,"recordsTotal": 57,"recordsFiltered": 57,"data":" + dataRez + "}";
}
return sJSONResponse;
}
And my Script
<!--Import jQuery before export.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<!--Data Table-->
<script type="text/javascript" src=" https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src=" https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>
<!--Export table buttons-->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.4/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.print.min.js"></script>
<!--Export table button CSS-->
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.2.4/css/buttons.dataTables.min.css">
<script type="text/javascript">
$(document).ready(function () {
$.noConflict();
$('#myTable1').DataTable({
"ajax": {
"url": "/Profile/GetData",
"type": "POST",
"datatype": "application/json"
},
"serverSide": "true",
"order": [0, "asc"],
"processing": "true",
dom: 'Bfrtip',
buttons: [
'copy', 'pdf', 'csv', 'print', 'excel'
]
});
});
</script>
add a comment |
This is how i fix it My Controller
[HttpPost]
public string GetData()
{
string sJSONResponse = "";
List<string> data = new List<string>();
using (var context = new RSAT.Api.Data.Proxy.ATT2018NOVUSContext())
{
var baseViewModel = (from userinfo in context.Userinfo
join department in context.Dept on userinfo.Deptid equals department.Deptid
select new
{
Id = userinfo.Userid,
Name = userinfo.Name,
Department = department.DeptName,
CardNumber = userinfo.CardNum,
Status = userinfo.UserFlag.ToString(),
HistoryOfStatuses = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).ToList(),
}).ToList();
foreach (var i in baseViewModel)
{
foreach (var h in i.HistoryOfStatuses)
{
var s = "["" + h.CheckStatus + ""," + """ + h.CheckTime + ""]";
data.Add(s);
}
}
string dataRez = "[";
foreach (var i in data)
{
dataRez += i + ",";
}
dataRez = dataRez.Remove(dataRez.Length - 1);
dataRez = dataRez + "]";
sJSONResponse = "{"draw": 1,"recordsTotal": 57,"recordsFiltered": 57,"data":" + dataRez + "}";
}
return sJSONResponse;
}
And my Script
<!--Import jQuery before export.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<!--Data Table-->
<script type="text/javascript" src=" https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src=" https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>
<!--Export table buttons-->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.4/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.print.min.js"></script>
<!--Export table button CSS-->
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.2.4/css/buttons.dataTables.min.css">
<script type="text/javascript">
$(document).ready(function () {
$.noConflict();
$('#myTable1').DataTable({
"ajax": {
"url": "/Profile/GetData",
"type": "POST",
"datatype": "application/json"
},
"serverSide": "true",
"order": [0, "asc"],
"processing": "true",
dom: 'Bfrtip',
buttons: [
'copy', 'pdf', 'csv', 'print', 'excel'
]
});
});
</script>
This is how i fix it My Controller
[HttpPost]
public string GetData()
{
string sJSONResponse = "";
List<string> data = new List<string>();
using (var context = new RSAT.Api.Data.Proxy.ATT2018NOVUSContext())
{
var baseViewModel = (from userinfo in context.Userinfo
join department in context.Dept on userinfo.Deptid equals department.Deptid
select new
{
Id = userinfo.Userid,
Name = userinfo.Name,
Department = department.DeptName,
CardNumber = userinfo.CardNum,
Status = userinfo.UserFlag.ToString(),
HistoryOfStatuses = (from checkinout in context.Checkinout
join status in context.Status on checkinout.CheckType equals status.Statusid
where checkinout.Userid == userinfo.Userid
orderby checkinout.CheckTime descending
select new Checkinout
{
CheckStatus = status.StatusText,
CheckTime = checkinout.CheckTime
}).ToList(),
}).ToList();
foreach (var i in baseViewModel)
{
foreach (var h in i.HistoryOfStatuses)
{
var s = "["" + h.CheckStatus + ""," + """ + h.CheckTime + ""]";
data.Add(s);
}
}
string dataRez = "[";
foreach (var i in data)
{
dataRez += i + ",";
}
dataRez = dataRez.Remove(dataRez.Length - 1);
dataRez = dataRez + "]";
sJSONResponse = "{"draw": 1,"recordsTotal": 57,"recordsFiltered": 57,"data":" + dataRez + "}";
}
return sJSONResponse;
}
And my Script
<!--Import jQuery before export.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<!--Data Table-->
<script type="text/javascript" src=" https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src=" https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>
<!--Export table buttons-->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.4/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.print.min.js"></script>
<!--Export table button CSS-->
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.2.4/css/buttons.dataTables.min.css">
<script type="text/javascript">
$(document).ready(function () {
$.noConflict();
$('#myTable1').DataTable({
"ajax": {
"url": "/Profile/GetData",
"type": "POST",
"datatype": "application/json"
},
"serverSide": "true",
"order": [0, "asc"],
"processing": "true",
dom: 'Bfrtip',
buttons: [
'copy', 'pdf', 'csv', 'print', 'excel'
]
});
});
</script>
answered Nov 23 '18 at 11:15
E.LahuE.Lahu
248
248
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411853%2fjquery-datatables-serverside-in-asp-mvc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
hi..are you sure to retunr View(viewModel) ? .. not a Jsonresult(viewModel) instead?
– federico scamuzzi
Nov 21 '18 at 12:46
Your AJAX URL seem to be wrong here. Also no need to return all instances of viewmodel, because you have only 2 columns in datatable.
– Tetsuya Yamamoto
Nov 22 '18 at 2:33