如何在不编写适配器的情况下将 JSON 数据从 MVC 4 传递到 Float
How to pass JSON data from MVC4 to Flot without writing an adapter
我正在为我的 MVC 控制器中的 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();
但是 none 他们飞了
恕我直言简单 data = new object { agent.Key, agent.Count()},
应该工作,但在构建时失败:Error 16 Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
.
我可以在检索数据后在 JS 中重塑数据,但是我想避免这个不必要的步骤。
如何将数据从 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"}});
}
希望这能为您节省一些时间
我正在为我的 MVC 控制器中的 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();
但是 none 他们飞了
恕我直言简单 data = new object { agent.Key, agent.Count()},
应该工作,但在构建时失败:Error 16 Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
.
我可以在检索数据后在 JS 中重塑数据,但是我想避免这个不必要的步骤。
如何将数据从 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"}});
}
希望这能为您节省一些时间