Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I was wondering how I can pass data to datatable.net in an asp.net mvc 4 application.

like this example: http://www.datatables.net/release-datatables/examples/data_sources/ajax.html

The output format I want is like this, but I can't figure out how to produce this result:

{
"aaData": [
[
  "Trident",
  "Internet Explorer 4.0",
  "Win 95+",
  "4",
  "X"
],
[
  "Trident",
  "Internet Explorer 5.0",
  "Win 95+",
  "5",
  "C"
]
]
}

So far I have something like this:

 public JsonResult GetDataList(int? assetId)
    {
        var tableData = DBData.ToList();

        var res = new JsonResult
        {
            JsonRequestBehavior = JsonRequestBehavior.AllowGet,
            Data = ??? // I must do something here with tableData
        };


        return res;

    }
share|improve this question

4 Answers 4

Try the below JsonResult, it should return the result in the format the datatables plugin is expecting.

public JsonResult GetDataList(int? assetId)
{
    var tableData = DBData.ToList();

    return Json(new {
        aaData = ???
    },
    "application/json", Encoding.UTF8, JsonRequestBehavior.AllowGet);
}

This should return your Json as below:

{
    "aaData": [
        {
          "Trident",
          "Internet Explorer 4.0",
          "Win 95+",
          "4",
          "X"
        },
        {
          "Trident",
          "Internet Explorer 5.0",
          "Win 95+",
          "5",
          "C"
        },
    ]
}
share|improve this answer
up vote 1 down vote accepted

I figured it out, my problem was I had to use an array of object (object[]), not a list

 public JsonResult GetData(int? Id)
    {
        List<sp_data_R_Result> tableData;
        if (assetId != null)
            tableData = DBData.Where(x => x.Id== Id).ToList();
        else
            tableData = DBData.ToList();

        var res = new JsonResult
        {
            JsonRequestBehavior = JsonRequestBehavior.AllowGet,
            Data = new
                    {
                        aaData = from d in tableData
                                 select new object[]
                                {
                                  d.col1,
                                  d.col2,
                                  ...
                                 }
                    }
        };

        return res;
    }
share|improve this answer

I assume you want to convert the datatable into json?? Please correct me if I m wrong. You can do it using json.net library:

http://james.newtonking.com/projects/json-net.aspx

string json = JsonConvert.SerializeObject(table, Formatting.Indented);
share|improve this answer
    
This does not work. I get {"aaData":"[\r\n {\r\n .. I need brackets... –  codea Jun 6 '13 at 9:22

For me this one worked:

return Json(new {
                    aaData = Json(list).Data
                },
                "application/json", JsonRequestBehavior.AllowGet);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.