我正在尝试从 int 类型的 JSON 数组中传递第二个变量。到目前为止,我只能通过 SP_NAME
(这是一个字符串)变量输出但现在我需要 SP_ID
也被传递出 Controller ,以便 jQuery 知道将信息放在哪里。
下面是 Controller 代码。有两行我认为是问题所在,我添加了 <------ 箭头来告诉你们。
[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();
}
所以在 SPRow
我正在获取所需的键和值的行是 (program1, 30)。我需要访问 SP_ID
在 JSON 返回中,这样我就可以像这样在我的 jQuery 中使用它。
<div class = "sp-label" title = "' + SP + '" sub_program_id="' + SP.SP_ID + '" onclick="sp_click(this)">' + SP + '</div>'
我以为我可以添加 item.SP_ID
至 SPMonthRow
像这样
SPMonthRow[item.SP_NAME, item.SP_ID] = SPContent;
但是,当我再次尝试构建时,我收到了这个错误
no overload for method 'this' takes 2 arguments
这个错误让我相信我错误地定义了字典,我的想法是正确的还是我偏离了正轨?有没有其他方法可以得到 SP_ID
进入 jQuery 中使用的 JSON 数组?老实说,我愿意接受任何和所有建议以获得 SP_ID
出那个 Controller 。我已经在谷歌上搜索了一段时间,不知道如何解决像我这样的问题。感谢您的帮助,如果您需要任何其他代码或信息来帮助诊断问题,请告诉我。
更新 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();
}
更新 2
这是此函数中使用的表的架构
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; }
}
更新 3
我尝试过使用动态结果类,但这对我不起作用,因为我无法使用 GameScripting 提供给我的代码。我不能使用它的原因是因为我需要使用 SQL DB 来为我提供数据。我无法对任何数据进行硬编码,因为它在不断变化。
我想要的只是能够返回SP_NAME
, SP_ID
以及 SPContent
中的金额通过 JSON。这样做的原因是在我的 jQuery 中我使用 SP_NAME
输出子程序的名称(例如 "subProgram1"
)。我用 SP_ID
让 jQuery 知道将子程序放在哪个程序下(例如,如果 SP_ID = 1
子程序将在程序 1 下)。 SPContent
中包含的金额是在 foreach
中输入的每月总计循环。
(program1 Jan Feb Mar
subProgram1 $100 $200 $300 etc...)`
这一切意味着我不能丢失这条线 SPMonthRow[item.SP_NAME] = SPContent;
除非我可以输出 SPContent
以另一种方式获取数据。我对所有解决方案都持开放态度,只要我能让数据从 JSON 中的函数流出,这样 jQuery 就可以显示我需要的内容。很抱歉这篇文章我只是想确保任何想要帮助我的人都拥有这样做所需的信息。请随时提出任何问题或提出任何代码请求,因为我经常检查我的问题状态。感谢您的帮助!
最佳答案
当使用 .NET 4.0+ 时,您可以使用动态类型将这两个值合并到一个新对象中并返回它,就像
dynamic result = new
{
Name = item.SP_NAME,
Id = item.SP_ID
};
return Json(result, JsonRequestBehavior.AllowGet);
更新:
我真的不明白你的代码是关于SP和MP的一些东西,它们似乎彼此有关系。因此,在您解释要归档的内容、查询代表的内容等之前,我无法为您的代码提供任何帮助。 我将尝试提供一些关于数组和字典的通用信息。
先来看看有哪些可用
JSON
{
"Key1":5,
"Key2":10,
"Key3":155
}
C#(您也可以使用 dictionary initialisation syntax )
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary.Add("Key1", 5);
dictionary.Add("Key2", 10);
dictionary.Add("Key3", 155);
这种结构称为字典,是一种简单的键值存储。你可以说:“这里我有一些数据,请将其保存为 program1
”。稍后你可以出去说:“你能给我保存为 program1
的数据使用吗”。
JSON
[
5,
15,
23,
78,
155,
4,
85
]
C#(initialisation syntax 可用)
int[] array = new int[6];
array[0] = 5;
array[1] = 15;
array[2] = 23;
array[3] = 78;
array[4] = 155;
array[5] = 4;
array[6] = 85;
这是一个数组,它只是值的累加,在累加中自动获得一个唯一的数字(这称为索引)。这些唯一编号从 0 开始,而不是 1。
您实际拥有的是一个字典,其中包含一个字符串键和一个数字数组作为实际数据。
JSON
{
"program1":[
101,
34,
67,
876
]
}
C#
Dictionary<string, int[]> dictionary = new Dictionary<string, int[]>();
int[] values = new int[4];
values[0] = 101;
values[1] = 34;
values[2] = 67;
values[3] = 876;
dictionary.Add("program1", values);
来看看你想要什么
你想要类似的东西
{
"program1", "30":[
101,
34,
67,
876,
44
7
]
}
那么,那是什么?一本字典?数组?您将如何访问 program1
元素?只是那个名字?但是“30”是什么?
如您所见,没有答案,这样的事情不存在。
我的解决方案
我认为您需要以下结构:
JSON
{
"program1":{
"30":[
34,
67,
876,
44,
36,
6
]
}
}
获取这种结构的 C# 代码类似于
dynamic data = new
{
program1 = new Dictionary<string, int[]>
{
{"30", new int[]{34,67,876,44,36,6}}
}
};
现在轮到您弄清楚您真正想要的是什么以及如何让您的代码正常工作了。快乐编码:)
更新2
如果你只是想要工作代码,而不了解实际发生了什么,试试这个:
[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];
List<dynamic> result = new List<dynamic>();
foreach (var item in SPRow)
{
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;
}
result.Add(
new {
Name = item.SP_NAME,
Id = item.SP_ID,
Content = SPContent
});
}
return Json(result, JsonRequestBehavior.AllowGet);
}
return View();
}
关于c# - 返回 JSON 数组中的第二个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12358399/