gpt4 book ai didi

c# - 如何在不编写适配器的情况下将 JSON 数据从 MVC 4 传递到 Float

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:02 26 4
gpt4 key购买 nike

我正在为我的 MVC Controller 中的 Jquery flot 生成数据,并希望使用更复杂的结构,以便我能够更改每个条形等的颜色。

为此我需要通过 data structure that looks following

{
label: "y = 3",
data: [[0, 3], [10, 3]]
}

当它是简单的数组时,我通过简单地获取数据并像下面那样塑造它来传递数据没有问题

var data = topAgents.Select(agent => new List<object>
{
agent.User != null ? string.Format("{0}", agent.User).Replace("SAGANTGB\\", "") : null,
agent.Amount
}).ToList();

return Json(data, JsonRequestBehavior.AllowGet);

在 javascript 方面:

function onAgentDataReceived(series) {
$("#agents").empty();
$.plot("#agents", [series], { bars: { show: true, barWidth: 0.6, align: "center" }, xaxis: { mode: "categories", tickLength: 0 } });
}

但是现在我似乎无法将数据塑造成符合 $.plot 期望的结构。

到目前为止我已经尝试过:

键值对:

var grouped = result.GroupBy(o => o.CREATE_USER_ID).Select(agent => new
{
data = new KeyValuePair<string,int>(
agent.Key != null ? string.Format("{0}", agent.Key).Replace("SAGANTGB\\", "") : null, agent.Count()
),
color = "yellow"
}).ToList();

具有属性的对象:

var grouped =  result.GroupBy(o => o.CREATE_USER_ID).Select(agent => new
{
data = new { A=
agent.Key != null ? string.Format("{0}", agent.Key).Replace("SAGANTGB\\", "") : null, B = agent.Count()
},
color = "yellow"
}).ToList();

名称值集合

var grouped =    result.GroupBy(o => o.CREATE_USER_ID).Select(agent => new
{
data = new NameValueCollection
{{
agent.Key != null ? string.Format("{0}", agent.Key).Replace("SAGANTGB\\", "") : null, agent.Count().ToString()}
},
color = "yellow"
}).ToList();

匿名对象数组

var grouped = result.GroupBy(o => o.CREATE_USER_ID).Select(agent => new
{
data = new object[] { agent.Key != null ? string.Format("{0}", agent.Key).Replace("SAGANTGB\\", "") : null, agent.Count()},
color = "yellow"
}).ToList();

但他们都没有飞

恕我直言,简单 data = new object { agent.Key, agent.Count()},应该工作,但它在构建时失败:错误 16 无效的匿名类型成员声明符。必须使用成员分配、简单名称或成员访问权限来声明匿名类型成员。

我可以在检索数据后在 JS 中 reshape 数据,但是我想避免这个不必要的步骤。

如何将数据从 MVC 传递到 javascript 而不必在 JS 端重新整形?

最佳答案

本题写了20分钟后,下次尝试成功

 var grouped =
result.GroupBy(o => o.CREATE_USER_ID).Select(agent => new
{
data = new List<List<object>>
{
new List<object>
{
agent.Key != null ? string.Format("{0}", agent.Key).Replace("SAGANTGB\\", "") : null,
agent.Count()
}
},
color = "yellow"
}).ToList();

return Json(grouped, JsonRequestBehavior.AllowGet);

显然您需要将其双重包装在列表中。

忘了说在 JS 端不再需要用数组包裹

 $.ajax({
url: '/dashboard/Home/CancellationAgents/',
type: "GET",
dataType: "json",
success: onCancellationAgentsReceived
});

function onCancellationAgentsReceived(series) {
$("#agentcancellations").empty();
$.plot("#agentcancellations", series, {
bars: { show: true, barWidth: 0.7, align: "center" }, xaxis: {mode: "categories", tickLength: 0, position: "bottom"}});
}

希望这可以节省你一些时间

关于c# - 如何在不编写适配器的情况下将 JSON 数据从 MVC 4 传递到 Float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37277957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com