这是我的控制器类:
public JsonResult FetchGraphDataJSON()
{
DataTable dt = FetchFirstDataTable();
List<GridModel> data = new List<GridModel>();
if (dt.Rows.Count != 0)
{
foreach (DataRow row in dt.Rows)
{
GridModel model = new GridModel();
model.ID = Convert.ToDouble(row["ID"].ToString());
model.SHORT_NAME = row["SHORT_NAME"].ToString();
model.CARRIER_NAME = row["CARRIER_NAME"].ToString();
model.GridSparklineModel = new List<GridSparklineModel>();
double carrierId = Convert.ToDouble(row["ID"].ToString());
DataTable dt2 = GetDt2();
foreach (DataRow item2 in dt2.Select("ID=" + carrierId))
{
model.GridSparklineModel.Add(new GridSparklineModel { YMDH = DateTime.Parse(item2["YMDH"].ToString()), SELL_DURATION = Convert.ToDouble(item2["SELL_DURATION"]) });
}
data.Add(model);
}
}
return Json(data, JsonRequestBehavior.AllowGet);
}
public DataTable FetchFirstDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(double));
dt.Columns.Add("SHORT_NAME");
dt.Columns.Add("CARRIER_NAME");
dt.Rows.Add(1, "A", "Carrier A");
dt.Rows.Add(2, "B", "Carrier B");
dt.Rows.Add(3, "C", "Carrier C");
dt.Rows.Add(4, "D", "Carrier D");
dt.Rows.Add(5, "E", "Carrier E");
dt.Rows.Add(6, "F", "Carrier F");
dt.Rows.Add(7, "G", "Carrier G");
return dt;
}
public DataTable GetDt2()
{
DataTable dt = new DataTable();
dt.Columns.Add("YMDH");
dt.Columns.Add("ID", typeof(double));
dt.Columns.Add("SELL_DURATION");
dt.Rows.Add("2009-03-07 00:00:00.000", 1, 222.999995);
dt.Rows.Add("2009-03-07 01:00:00.000", 1, 75.816664);
dt.Rows.Add("2009-03-07 02:00:00.000", 1, 39.349995);
dt.Rows.Add("2009-03-07 03:00:00.000", 1, 275.91666);
dt.Rows.Add("2009-03-07 04:00:00.000", 1, 352.666641);
dt.Rows.Add("2009-03-07 00:00:00.000", 2, 80.783324);
dt.Rows.Add("2009-03-07 01:00:00.000", 2, 162.049985);
dt.Rows.Add("2009-03-07 02:00:00.000", 2, 107.199989);
dt.Rows.Add("2009-03-07 03:00:00.000", 2, 44.849994);
dt.Rows.Add("2009-03-07 04:00:00.000", 2, 156.516658);
dt.Rows.Add("2009-03-07 05:00:00.000", 2, 467.583312);
dt.Rows.Add("2009-03-07 06:00:00.000", 2, 455.199977);
return dt;
}
CSHTML
<script>
$(document).ready(function () {
var url = '@Url.Action("FetchGraphDataJSON", "Grid")';
var rawData = null;
$.ajax({
// have to use synchronous here, else the function
// will return before the data is fetched
async: false,
url: url,
dataType: "json",
success: function (data) {
rawData = data;
}
});
var plotData = []
for (var i = 0; i < rawData.length; i++) {
//Parse the date.
document.write(rawData[i] + "<br>");
}
});
</script>
运行它时会出现错误:编译器错误消息:CS1647:表达式太长或太复杂而无法编译。知道如何解决这个问题。我使用vs2012与mvc和c#。