我是 asp.net mvc3 的新手,并且一直在阅读有关 MVC3 razor 图表的 stackoverflow 中的很多帖子。
但是,我无法为此创建图表。基本上这是我的 sql 查询
SELECT COUNT(RequestStatus) AS Expr1, RequestStatus
FROM Request
GROUP BY RequestStatus
到目前为止,在我的控制视图中,这是我的代码:
public ActionResult RequestReport()
{
var result = from p in db.Requests
group p by p.RequestStatus into pgroup
let count = pgroup.Count()
orderby count
select new { Count = count, RequestStatus = pgroup.Key }.ToString();
var x = new [] {result};
Chart chart = new Chart(600, 400)
.AddSeries(yValues: x , yFields:"Count")
.AddTitle("Request Statistic");
this.ViewBag.Chart = chart;
return View();
}
并在视图中:
@ViewBag.Chart.Write()
请帮我解决这个问题。我找不到一个简单的教程。
我对控制器中的代码进行了一些更改。
using (var db = new ITAMMVCDBEntities())
{
var rs = db.Requests.GroupBy(o=>o.RequestStatus).ToList();
var result = from o in db.Requests
group o by o.RequestStatus
into g
select new
{
result = g.Count()
};
Chart chart = new Chart(600, 400)
.AddSeries(
xValue: rs.Select(x=>x.Key).ToArray() ,
yValues: result.ToArray()
)
.AddTitle("Request Statistic");
this.ViewBag.Chart = chart;
return View();
}
现在我在运行时收到此错误消息
Series data points do not support values of type <>f__AnonymousType1`1[System.Int32] only values of these types can be used: Double, Decimal, Single, int, long, uint, ulong, String, DateTime, short, ushort.
我认为我的 xValue 没问题,因为它按组显示,但问题 populationg yValue 将显示每个组的值