title: Return Multiple Values using Json, jQuery and Ajax
body: <p>I am trying to use two linq-to-SQL statements to return data to an array from a database that I can use in a jQuery call. I can get the Json to return one array but I cannot get it to return the second array that contains the amounts that I need to list.</p>
  
  <p>You see what I am doing is I am creating a treeview that lists Areas, projects and subprograms for each section. Every Area has a total amount spent, and every project has a total amount spent and every subprogram has a total amount spent. I have been able to get the total amount spent for the subprograms but the projects are a different story since I must use the same controller that I use to display the subprograms to display the totals for each project. It looks a little like this.</p>
  
  <p><code>Area------------Jan--Feb--March--April--May--June--July--Aug--Sept--Oct--Nov--Dec</code></p>
  
  <p><code>+Project--------Jan--Feb--March--April--May--June--July--Aug--Sept--Oct--Nov--Dec</code></p>
  
  <p><code>++Sub-Program1--Jan--Feb--March--April--May--June--July--Aug--Sept--Oct--Nov--Dec</code></p>
  
  <p><code>++Sub-Program2--Jan--Feb--March--April--May--June--July--Aug--Sept--Oct--Nov--Dec</code></p>
  
  <p>Since the project amount is a sum of the subprogram amount and is in a different table than where the sub-programs are listed I am having difficulty getting the second array filled. The code below is from my controller and shows how I pull the data for the array that fills int the Sub-Programs names.</p>
  
  <pre><code>[Authorize]
          public virtual ActionResult getAjaxPGs(string SP = null, string PG = null)
          {
  
              if (SP != null)
              {
                  var PGList = from x in db.pg_mapping
                               where x.PG_SUB_PROGRAM == SP 
                               select x;
                                   //select x.PG.Distinct().ToArray(); // I wanted to combine the PGRow = line below with the linq to SQL code above but it didn't like the .ToArray() 
  
  
                  var PGRow = PGList.Select(x =&gt; new { x.PG }).Distinct().ToArray();
  
                  return Json(PGRow, JsonRequestBehavior.AllowGet);
  
              }
  
  
              return View();
          }
  </code></pre>
  
  <p>So this code here finds and outputs the sub-programs names in place of Sub-Program1 and Sub-Program2. This part need to stay so that it will output the correct subprogram names but now I need to add code that will output the amount total at the project level. I thought that I could just add this code to the controller and return two arrays but I am not doing it right and none of the ways I have tried has worked. </p>
  
  <p><code>var AmountList = from x in db.Spend_Web
                                  where x.PG == PG
                                  group x by new { x.ACCOUNTING_PERIOD, x.PG, x.Amount } into pggroup
                                  select new { accounting_period = pggroup.Key.ACCOUNTING_PERIOD, amount = pggroup.Sum(x =&gt; x.Amount) };</code></p>
  
  <p>and then return it like this</p>
  
  <p><code>return Json(PGRow, AmountList, JsonRequestBehavior.AllowGet);</code></p>
  
  <p>This did not work it threw an error that said <code>The best overload match for 'System.Web.MVC.Controller.Json(object,string,System.Web.MVC.JsonRequestBehavior)' has some invalid arguments</code></p>
  
  <p>I am completely open to all suggestions since everything I have tried has not worked; even if a suggestion takes me into a completely new direction. I honestly just need some guidance on how to go about achieving the desired result. I hope I have expressed my question clearly. Thank you all for your time and expertise, please don't hesitate to ask for any extra code or clarification as I will be checking back as often as possible.   </p>
  
  <p><strong>Update</strong></p>
  
  <pre><code>public class Container
          {
              Array PARow;
              Array AmountList;
  
          }
  
  public virtual ActionResult getAjaxPGs(string SP = null, string PG = null)
              {
                  var PGList = from x in db.pg_mapping
                               where x.PG_SUB_PROGRAM == SP
                               select x;
  
                  container.PGRow = PGList.Select(x =&gt; new { x.PG }).Distinct().ToArray();
  
                  container.AmountList = from x in db.Spend_Web
                                      where x.PG == PG
                                      group x by new { x.ACCOUNTING_PERIOD, x.PG, x.Amount } into pggroup
                                      select new { accounting_period = pggroup.Key.ACCOUNTING_PERIOD, amount = pggroup.Sum(x =&gt; x.Amount) };
  
  
              }
  </code></pre>