简体   繁体   中英

Return Multiple Values using Json, jQuery and Ajax

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.

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.

Area------------Jan--Feb--March--April--May--June--July--Aug--Sept--Oct--Nov--Dec

+Project--------Jan--Feb--March--April--May--June--July--Aug--Sept--Oct--Nov--Dec

++Sub-Program1--Jan--Feb--March--April--May--June--July--Aug--Sept--Oct--Nov--Dec

++Sub-Program2--Jan--Feb--March--April--May--June--July--Aug--Sept--Oct--Nov--Dec

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.

[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 => new { x.PG }).Distinct().ToArray();

                return Json(PGRow, JsonRequestBehavior.AllowGet);

            }


            return View();
        }

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.

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 => x.Amount) };

and then return it like this

return Json(PGRow, AmountList, JsonRequestBehavior.AllowGet);

This did not work it threw an error that said The best overload match for 'System.Web.MVC.Controller.Json(object,string,System.Web.MVC.JsonRequestBehavior)' has some invalid arguments

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.

Update

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 => 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 => x.Amount) };


            }

Create a "container" class that holds an instance of each datatype you wish to return.

class Container
{
    Array PGRow;
    Array AmountList;
}

Then in your code:

Container container = new Container();
container.PGRow = ....;
container.AmountList = ....;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM