Using Datatables with MVC4 and WebAPI

Using Datatables with MVC4 and WebAPI

FredFlintstoneFredFlintstone Posts: 24Questions: 0Answers: 0
edited November 2012 in General
hi - i have been trying for a couple of days (i know!) to wire up a datatable with json returned from an apicontroller without success. was wondering if anyone here has managed to do this. among other things, i have tried the approach suggested here:

http://www.briankeating.net/post/2012/07/08/Using-DataTablesnet-with-Asp-MVC-Web-Api.aspx

EDIT: the approach advocated is:

[code]
$('#example').dataTable({
"bJQueryUI": true,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/api/myapicontroller/get"
});
[/code]

without success. i can't even make the call hit the controller.

also tried $.getJSON which does call the controller but the datatable resolutely refuses to render out the returned JSON. :(

Replies

  • allanallan Posts: 63,532Questions: 1Answers: 10,475 Site admin
    1. Have you fully implemented server-side processing in your controller? http://datatables.net/usage/server-side (since you've enabled it in DataTables, it expects a valid reply).

    2. Please link us to a test page showing the issue.

    Allan
  • FredFlintstoneFredFlintstone Posts: 24Questions: 0Answers: 0
    hi allan, thanks for your reply. unfortunately i can't post a link to a page as this is still very much in development. my controller signature looks like

    public IEnumerable Get(int sEcho,
    int iDisplayStart,
    int iDisplayLength,
    string sSearch,
    int iSortCol_0,
    string sSortDir_0)

    but this simply isn't being hit.
  • allanallan Posts: 63,532Questions: 1Answers: 10,475 Site admin
    Is the browser sending an XHR? If not then is there a Javascript error on the page? If it is, then we can't really help here as this isn't a WebAPI forum.
  • FredFlintstoneFredFlintstone Posts: 24Questions: 0Answers: 0
    fiddler indicates that nothing is being sent. all very odd.
  • FredFlintstoneFredFlintstone Posts: 24Questions: 0Answers: 0
    and no javascript error
  • allanallan Posts: 63,532Questions: 1Answers: 10,475 Site admin
    In that case I've no idea I'm afraid. We'd really need to be able to see a test case since it could be almost anything.

    Allan
  • FredFlintstoneFredFlintstone Posts: 24Questions: 0Answers: 0
    just a thought - is there a requirement for the datatable to be hosted on a file with a specific extension eg .html, .aspx etc? just wondering as i can't get even the most basic of samples to work and the table lives in a .cshtml file
  • allanallan Posts: 63,532Questions: 1Answers: 10,475 Site admin
    No - absolutely not. As long as you can run Javascript, you can run DataTables. I presume you can run Javascript? Can you run the DataTables debugger on the page?

    Allan
  • FredFlintstoneFredFlintstone Posts: 24Questions: 0Answers: 0
    no - i get this:


    DataTables debug bookmarklet
    DataTables is not available on this page.

    clearly something is badly wrong with my configuration :(
  • FredFlintstoneFredFlintstone Posts: 24Questions: 0Answers: 0
    hi allan,

    got it! it was the mvc4 way of bundling scripts that was improperly configured. sorry for wasting your time on this...
This discussion has been closed.