0

Main thing is i am new in Jquery Datatable and from last 2 days I am trying to use it, using json string. But i am not getting require output.

Here is my HTML :

 <body>
    <div class="container">
        <table id="MydataTableDiv">
            <thead>
              <tr>
               <th>NAME</th>
               <th>MARKS</th>
              </tr>
            </thead>
        </table>
    </div>
</body>

Here is my Script :

 $(document).ready(function () {
    $("#MydataTableDiv").DataTable({
        serverSide: true,
        processing: true, 
        ajax: {
            "url": "../Home/GetData",
            "dataSrc":''
        },
        columns: [
            { data: "NAME" },
            { data: "MARKS" }
        ],
    });
});

And finally , My Home Controller :

public JsonResult GetData()
    {
        DataTable _dt = new DataTable();

        _dt.Columns.Add("NAME");
        _dt.Columns.Add("MARKS");

        for (int i = 0; i < 10; i++)
        {
            DataRow _dr = _dt.NewRow();
            _dr["NAME"] = "A_" + i;
            _dr["MARKS"] = i * 10;
            _dt.Rows.Add(_dr);
        }

        string JsonResult = JsonConvert.SerializeObject(_dt);
        return Json(new {data=JsonResult }, JsonRequestBehavior.AllowGet);
    }

Here is my Json string returned from controller :

[{"NAME":"A_0","MARKS":"0"},{"NAME":"A_1","MARKS":"10"},{"NAME":"A_2","MARKS":"20"},{"NAME":"A_3","MARKS":"30"},{"NAME":"A_4","MARKS":"40"},{"NAME":"A_5","MARKS":"50"},{"NAME":"A_6","MARKS":"60"},{"NAME":"A_7","MARKS":"70"},{"NAME":"A_8","MARKS":"80"},{"NAME":"A_9","MARKS":"90"}]

This code gives me a blank and empty table which contains all paging, filteration options. please help me what is mistake in above code or is there any other way to use it for dynamic data.

5
  • can you verify what is the response from Ajax call in your network tab of developer tools? Is it what your datatable is expecting? Commented Mar 18, 2018 at 14:28
  • @G_S it is given me , c# datatable in json format. Commented Mar 20, 2018 at 10:34
  • Is that what a datatable expects? can you post what you got in JSON format to your question if possible. (Because I expect that JSON is not what a datatable expects) Commented Mar 20, 2018 at 13:28
  • @G_S I updated my question. Commented Mar 20, 2018 at 14:30
  • Check the answer provided and let me know if it works for you Commented Mar 20, 2018 at 17:34

1 Answer 1

1

From what I have observed, datatables need a JSON object rather than a string. You are returning a string which can be converted to a JSON.

Made small change to your ajax call

ajax: {
            url: "http://localhost:21594/api/values",
            "dataSrc": function ( json ) {
                return JSON.parse(json);
            }    
        },

The above converts your data which you are returning as string to a JSON which datatable understands

Sign up to request clarification or add additional context in comments.

2 Comments

thanx @G_S , I tried json.parse() but it shows wrong json format message.
So your json is in incorrect format then. verify your JSON again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.