Datatables warning: JSON Data - Using MVC and LINQ as server side data

Datatables warning: JSON Data - Using MVC and LINQ as server side data

hermes980hermes980 Posts: 41Questions: 13Answers: 0
edited March 2014 in General
Hi all,

I need to use MVC/LINQ to provide the server side data for my datatables. How do I change the following controller to work?

[code]
public ActionResult Index()
{
var histories = db.Histories.Include(a => a.ActivityType);
return View(histories.ToList());
}
[/code]

I have seen this, but don't know how to include my data:

[code]
public class HomeController : Controller {
[HttpPost]
public ActionResult GetDataTables(DataTable dataTable) {
List table = new List();
//Do something with dataTable and fill table
return new DataTableResult(dataTable, table.Count, table.Count, table);
}
}
[/code]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    This isn't a .NET forum - I think you probably won't get an answer here. All I can really advise you to do is output JSON - but how you do that... Ask in a .NET forum :-)

    Allan
  • MrBaseball34MrBaseball34 Posts: 96Questions: 0Answers: 0
    You need to convert your List to the correct JSON data. I use StringBuilder as it's much faster than concatenation:
    [code]
    // Rows is a List
    StringBuilder sb = new StringBuilder("{\"aaData\":[ ");
    for (int i = 0; i <= Rows.Count() - 1; i++)
    {
    sb.Append("{");
    // Add each column's data
    sb.Append(Rows[i]);
    if (i < Rows.Count - 1)
    {
    sb.Append("},");
    }
    else
    {
    sb.Append("}");
    }
    sb.Append("]}");
    }
    return sb.ToString();
    [/code]
This discussion has been closed.