这是我的Model Class代码:
public class GridModel
{
public double ID { get; set; }
public string SHORT_NAME { get; set; }
public string CARRIER_NAME { get; set; }
public List<GridSparklineModel> SellDuration { get; set; }
}
public class GridSparklineModel
{
public DateTime YMDH { get; set; }
public double SELL_DURATION { get; set; }
public string DateString { get { return YMDH.ToString("h tt"); } }
}
------------------ controller ---------------------
public class GridController:Controller { // // GET:/ Grid /
public ActionResult Index()
{
return View();
}
public class JsonNetActionResult : ActionResult
{
public Object Data { get; private set; }
public JsonNetActionResult(Object data)
{
this.Data = data;
}
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ContentType = "application/json";
context.HttpContext.Response.Write(JsonConvert.SerializeObject(Data));
}
}
public JsonNetActionResult FetchGraphDataJSON()
{
List<GridModel> data = new List<GridModel>();
//DataFetching Logic
DataTable dt = FetchFirstDataTable();
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.SellDuration = new List<GridSparklineModel>();
double carrierId = Convert.ToDouble(row["ID"].ToString());
DataTable dt2 = GetDt2();
foreach (DataRow item2 in dt2.Select("ID=" + carrierId))
{
model.SellDuration.Add(new GridSparklineModel { YMDH = DateTime.Parse(item2["YMDH"].ToString()), SELL_DURATION = Convert.ToDouble(item2["SELL_DURATION"]) });
}
data.Add(model);
}
}
return new JsonNetActionResult(data);
}
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 (i = 0; i < rawData.length; i++) {
// This has to be modified such that it builds the JSON like this [{"ID":1,"SHORT_NAME":"A","CARRIER_NAME":"Carrier A","SellDuration":[{"YMDH":"2009-03-07 00:00:00.000","SELL_DURATION":222.999995},{"YMDH":"2009-03-07 05:00:00.000","SELL_DURATION":275.91666}]}];
plotData.push([rawData[i].ID, rawData[i].SHORT_NAME, rawData[i].CARRIER_NAME], [rawData[i].SellDuration]);
}
});
</script>
有人可以建议我如何编写上面的语句,以便它像这样构建JSON: var plotData = [{&#34; ID&#34;:1,&#34; SHORT_NAME&#34;:&#34; A&#34;,&#34; CARRIER_NAME&#34;:&#34;运营商A&# 34;,&#34; SellDuration&#34;:[{&#34; YMDH&#34;:&#34; 2009-03-07 00:00:00.000&#34;,&#34; SELL_DURATION&#34; :222.999995},{&#34; YMDH&#34;:&#34; 2009-03-07 01:00:00.000&#34;,&#34; SELL_DURATION&#34;:75.816664},{&#34; YMDH&#34;:&#34; 2009-03-07 02:00:00.000&#34;,&#34; SELL_DURATION&#34;:39.349995},{&#34; YMDH&#34;:&#34; 2009-03-07 03:00:00.000&#34;,&#34; SELL_DURATION&#34;:75.816664},{&#34; YMDH&#34;:&#34; 2009-03-07 04:00: 00.000&#34;,&#34; SELL_DURATION&#34;:39.349995},{&#34; YMDH&#34;:&#34; 2009-03-07 05:00:00.000&#34;,&#34 ; SELL_DURATION&#34;:275.91666}]}, {&#34; ID&#34;:1,&#34; SHORT_NAME&#34;:&#34; B&#34;,&#34; CARRIER_NAME&#34;:&#34;运营商A&#34;,& #34; SellDuration&#34;:[{&#34; YMDH&#34;:&#34; 2009-03-07 00:00:00.000&#34;,&#34; SELL_DURATION&#34;:222.999995}, {&#34; YMDH&#34;:&#34; 2009-03-07 01:00:00.000&#34;,&#34; SELL_DURATION&#34;:75.816664},{&#34; YMDH&#34; :&#34; 2009-03-07 02:00:00.000&#34;,&#34; SELL_DURATION&#34;:39.349995},{&#34; YMDH&#34;:&#34; 2009-03- 07 03:00:00.000&#34;,&#34; SELL_DURATION&#34;:75.816664},{&#34; YMDH&#34;:&#34; 2009-03-07 04:00:00.000&#34 ;,&#34; SELL_DURATION&#34;:39.349995},{&#34; YMDH&#34;:&#34; 2009-03-07 05:00:00.000&#34;,&#34; SELL_DURATION&#34 ;:275.91666}]} ];
请告知。