I am trying to pass a second variable out of a JSON array that is of type int. So far I have only been able to pass the SP_NAME
(which is a string) variable out but now I need the SP_ID
to be passed out of the controller as well so the jQuery knows where to place the information.
Below is the controller code. There are two lines where I think the trouble is coming from and I have added <------ arrows to show you guys.
[Authorize]
public virtual ActionResult getAjaxSPs(string MP = null)
{
if (MP != null)
{
var SPList = from x in sp_index select x;
var MPData = from y in mp_index
where y.MP_NAME == MP
select y;
SPList = SPList.Where(x => x.MP_ID == MPData.FirstOrDefault().MP_ID);
var SPRow = SPList.Select(x => new { x.SP_NAME, x.SP_ID }).Distinct().ToArray(); // <------- SPRow returns two variable SP_NAME and SP_ID (ex. Program1, 30)
float[] SPContent = new float[12];
Dictionary<string, float[]> SPMonthRow = new Dictionary<string, float[]>();
foreach (var item in SPRow)
{
SPContent = new float[12];
var SPMonthList = from x in Full_Year_Numbers
where x.PROJECT_NAME == item.SP_NAME
group x by new { x.ACCOUNTING_PERIOD, x.PROJECT_NAME, x.AMOUNT } into spgroup
select new { accounting_period = spgroup.Key.ACCOUNTING_PERIOD, amount = spgroup.Sum(x => x.AMOUNT) };
foreach (var mulan in SPMonthList)
{
int acounting_period = int.Parse(mulan.accounting_period) - 1;
SPContent[acounting_period] = (float)mulan.amount / 1000000;
}
SPMonthRow[item.SP_NAME] = SPContent;
}
return Json(SPMonthRow, JsonRequestBehavior.AllowGet); //<--------- This is where I need to return two variables so I can use them both in the Jquery
}
return View();
}
So in the SPRow
line I am getting the key and the values that I need which are (program1, 30). I need to have access to the SP_ID
in the JSON return so I can use it in my jQuery like this.
<div class = "sp-label" title = "' + SP + '" sub_program_id="' + SP.SP_ID + '" onclick="sp_click(this)">' + SP + '</div>'
I thought that I could just add item.SP_ID
to SPMonthRow
like this
SPMonthRow[item.SP_NAME, item.SP_ID] = SPContent;
But, when I tried to build again I received this error
no overload for method 'this' takes 2 arguments
This error leads me to believe that I defined the dictionary incorrectly, am I correct in thinking this or am I off track? Is there another way to get the SP_ID
into the JSON array that is being used in the jQuery? I am honestly open to any and all suggestions to get SP_ID
out of that controller. I have been googling for a while now with no idea on how to fix an issue like mine. Thanks for your help and please let me know if you need any other code or information to help with diagnosis of the issue.
Update 1
foreach (var item in SPRow)
{
SPContent = new float[12];
var SPMonthList = from x in act.WPD_Full_Year_Actuals
where x.PROJECT_NAME == item.SP_NAME
group x by new { x.ACCOUNTING_PERIOD, x.PROJECT_NAME, x.AMOUNT } into spgroup
select new { accounting_period = spgroup.Key.ACCOUNTING_PERIOD, amount = spgroup.Sum(x => x.AMOUNT) };
foreach (var mulan in SPMonthList)
{
int accounting_period = int.Parse(mulan.accounting_period) - 1;
SPContent[accounting_period] = (float)mulan.amount / 1000000;
}
dynamic result = new
{
Name = item.SP_NAME,
Id = item.SP_ID
};
SPMonthRow[result] = SPContent;
}
return Json(SPMonthRow, JsonRequestBehavior.AllowGet);
}
return View();
}
Update 2
Here is the schema of the tables used in this function
public partial class sp_catalog
{
public int SP_ID { get; set; } //primary key
public Nullable<int> MP_ID { get; set; }
public Nullable<int> IA_ID { get; set; }
public string SP_NAME { get; set; }
}
public partial class mp_catalog
{
public int MP_ID { get; set; } //primary key
public Nullable<int> IA_ID { get; set; }
public string MP_NAME { get; set; }
}
public partial class Full_Year_Actuals
{
public string ACCOUNTING_PERIOD { get; set; }
public Nullable<decimal> AMOUNT { get; set; }
public string PROJECT_NAME { get; set; }
public int ID_actuals { get; set; }
}
Update 3
I have tried using the dynamic result class but that does not work for me since I cannot use the code GameScripting provided me with. The reason I cannot use it is because I need to work with the SQL DB to provide me with the data. I cannot hard code any data as it is constantly changing.
All I want is to be able to return SP_NAME
, SP_ID
and the amounts in SPContent
through JSON. The reason for this is that in my jQuery I use SP_NAME
to output the name of the Sub-Program (ex. "subProgram1"
). I use SP_ID
to let the jQuery know which program to put the sub-progrm under (ex. if SP_ID = 1
the sub-program would be under program1). The amounts that are contained within SPContent
are monthly totals that are entered in the foreach
loop.
(program1 Jan Feb Mar
subProgram1 $100 $200 $300 etc...)`
What this all means is I cannot lose this line SPMonthRow[item.SP_NAME] = SPContent;
unless I can output SPContent
amount data another way. I am open to all solutions as long as I can keep the data flowing out of the function in JSON so the jQuery can display what I need it to. Sorry for the essay I just want to make sure anyone who wants to help me has the information required to do so. Please feel free to ask any questions or make any request for code as I check the status of my questions often. Thank you for your help!