Using DataTables ServerSide with .Net MVC
Using DataTables ServerSide with .Net MVC
So I am trying to get my table loaded server side. I will show my code, with the errors I am receiving.
Controller Method:
[HttpPost("Admin/Postings")]
public JsonResult GetAllUsers()
{
JsonResult result = new JsonResult(new { });
// Initialization.
string search = Request.Form.GetValues("search[value]")[0];
string draw = Request.Form.GetValues("draw")[0];
string order = Request.Form.GetValues("order[0][column]")[0];
string orderDir = Request.Form.GetValues("order[0][dir]")[0];
int startRec = Convert.ToInt32(Request.Form.GetValues("start")[0]);
int pageSize = Convert.ToInt32(Request.Form.GetValues("length")[0]);
// Loading.
List<PostingViewModel> data = _adminServices.GetAllPostings();
// Total record count.
int totalRecords = data.Count;
data = data.Skip(startRec).Take(pageSize).ToList();
var modifiedData = data.Select(d =>
new
{
d.BrandLogo,
d.Title,
d.BrandName,
d.LocationName,
d.NumberOfApplications.New
}
);
result = Json(new
{
draw = Convert.ToInt32(draw),
recordsTotal = totalRecords,
//recordsFiltered = recFilter,
data = modifiedData
}, JsonRequestBehavior.AllowGet);
// Return info.
return result;
}
GetValues() error: IFormCollection does not contain a definition for GetValues and no accessible extension method GetValues accepting a first argument of type IFormCollection could be found
_adminServices error: Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<DataModel.Models.Posting>>' to 'System.Collections.Generic.List<ViewModels.ViewModels.PostingViewModel>'
Here is my GetAllPostings() method (no errors):
public async Task<IEnumerable<Posting>> GetAllPostings()
{
try
{
IQueryable<Posting> query = _unitOfWork
.PostingRepository
.All("Location", "Location.Company", "Category", "Applications", "LanguageTag")
.OrderBy(e => e.Title);
IEnumerable<Posting> postings = await _unitOfWork
.PostingRepository
.Materialize(query);
return postings;
}
catch (Exception ex)
{
throw new ServiceException(ServiceExceptionType.UnexpectedError, ex.Message, ex);
}
}
I can post up my models if required. My table works fine on client side, but I am needing to switch to server side now. Any help is appreciated!
Answers
Convert modified data in
var data = modifieddata.Skip(skip).Take(pageSize).ToList();
and pass it to view. (skip and page size come from view)
@freecex Sorry, I'm not sure what you mean by "Convert modified data in"
What line should I be putting: var data = modifieddata.Skip(skip).Take(pageSize).ToList(); ?
Sorry...t9.
After
var modifiedData = data.Select(d =>
new
{
d.BrandLogo,
d.Title,
d.BrandName,
d.LocationName,
d.NumberOfApplications.New
}
);
before json result,
put
var DTLIST= modifieddata.Skip(skip).Take(pageSize).ToList();
and
result = Json(new
{
draw = Convert.ToInt32(draw),
recordsTotal = totalRecords,
//recordsFiltered = recFilter,
data = DTLIST
}, JsonRequestBehavior.AllowGet);
Thanks @freecex
Do you have any ideas on solving my errors? The GetValues and ToList errors are what's stopping me from running the program.
Hi,
This discussion helped me to setup MVC Controller correctly for server side pagination:
https://datatables.net/forums/discussion/27216/how-to-get-the-parameters-on-mvc-controller-send-by-ajax-serverside-datatable-1-10
See the fatih's post in this discussion.
Cheers,
Sašo