user2739418
user2739418

Reputation: 1631

Return Multiple Json Objects from MVC (Ajax/Jquery)

Hi,

We are using MVC/JQuery for one web application: In one the screen users can generate charts. Now requirement as below:

1) Select Chart Criteria. (Client/Date Range/Chart Axis etc)

2) Press “Generate” Button… Which further makes a call to JQuery and then get the Data from Server through Ajax Call.

3)

public JsonResult GetDayData([DataSourceRequest]DataSourceRequest request,
      string clientID, DateTime? startDate, DateTime? endDate, string SortOrder))
{
    //Makes a call to Stored Procedure
    Stored procedure runs and return multiple datatables (ds.tavles[0], ds.tables[1] etc.
}

4) Based on these datatables we need to refresh/create 4-5 charts (assuming 4-5 datatables will be returned from stored procedure)

5) I am able to return dataTable[0] and create chart but I am not able to find how to return multiple Json objects in one call and how to handle those in JQuery/Ajax.

One approach is to make separate for each chart controller and return Json objects but I don’t want to make multiple calls to Stored procedure.

Any suggestions how I can return multiple dataTable/or class objects from MVC to Ajax Call?

e.g: Each Json objects can have a same or different structure
DataTable 1: {Category: “A”, Points:20, Percentage:87} 
        {Category: “B”, Points:20, Percentage:87} 

DataTable 1: {Category: “A”, Spots:20, Percentage:87,ExtraInfo: “NA”} 
        {Category: “B”, Spots:20, Percentage:87,ExtraInfo: “NA”} 

Thanks

Upvotes: 1

Views: 4879

Answers (2)

DK Chauhan
DK Chauhan

Reputation: 194

I am suggesting you to try in this way

[HttpPost]
public JsonResult Action()
    {
        MyClass objMyClass = new MyClass();
        objMyClass.lstTables = new List<System.Data.DataTable>();
        objMyClass.lstTables.Add(new System.Data.DataTable());
        objMyClass.lstTables.Add(new System.Data.DataTable());
        return Json(objMyClass);
    }
    public class MyClass
    {
        public List<System.Data.DataTable> lstTables { get; set; }
    }

Now you can pass n number of tables

Upvotes: 1

Anton Krutikov
Anton Krutikov

Reputation: 56

You can return array in one JSON object (do it in application or return one result obkect from database if you like), like:

{
  [
    {data for table 1},
    {data for table 2},
    ...
  ]
}

Or if it different types, like

{
  [
    {ChartType1: Type1, Data: {data for table 1}},
    {ChartType2: Type2, Data: {data for table 2}},
    ...
  ]
}

Upvotes: 0

Related Questions