如何将数据绑定到MVC3中的高图中的折线图?

时间:2012-08-28 13:00:42

标签: asp.net-mvc-3 highcharts linechart

  

可能重复:
  How to bind data to line chart in highcharts in MVC3?

我的实体框架中有两个程序工作...以及以json的形式返回数据的方法。我在单个方法中调用这两个程序,我必须将这两个程序作为单个json对象返回... ..并将此数据返回到我的jquery中的。$ getJson方法...可以告诉我如何执行此操作并且返回的数据应绑定到highcharts的Linechart,因为两个单独的行可以告诉我如何才能实现此

   public ActionResult LoggedBugs()
    {
        return View();  
    }

    public JsonResult CreatedBugs()
    {
        int year;
        int month;
        int projectid;
        year=2012;
        month=8;
        projectid=16;
        var loggedbugs = db.ExecuteStoreQuery<LoggedBugs>("LoggedBugs @Year,@Month,@ProjectID", new SqlParameter("@Year", year), new SqlParameter("@Month", month), new SqlParameter("@ProjectID", projectid)).ToList();
        var ClosedBugs= db.ExecuteStoreQuery<ClosedBugs>("ClosedBugs @Year,@Month,@ProjectID", new SqlParameter("@Year", year), new SqlParameter("@Month", month), new SqlParameter("@ProjectID", projectid)).ToList();
        return Json(loggedbugs, JsonRequestBehavior.AllowGet);
    }

我想将logbugs和Closedbugs作为json对象返回到我的视图,并且从那里我必须将这些数据绑定到Linechart ...其中loggedbugs应该有一行,而Closedbugs应该有其他行....期待这里的帮助< / p>

1 个答案:

答案 0 :(得分:0)

与MVC应用程序一样,首先定义一个视图模型,该模型将包含您的视图所需的信息(在您的情况下,这将是已记录和已关闭的错误列表):

public class BugsViewModel
{
    public string IEnumerable<LoggedBugs> LoggedBugs { get; set; }
    public string IEnumerable<ClosedBugs> ClosedBugs { get; set; }
}

然后让控制器操作填充将传递给视图的视图模型:

public ActionResult CreatedBugs()
{
    var year = 2012;
    var month = 8;
    var projectid = 16;
    var loggedbugs = db.ExecuteStoreQuery<LoggedBugs>("LoggedBugs @Year,@Month,@ProjectID", new SqlParameter("@Year", year), new SqlParameter("@Month", month), new SqlParameter("@ProjectID", projectid)).ToList();
    var closedBugs = db.ExecuteStoreQuery<ClosedBugs>("ClosedBugs @Year,@Month,@ProjectID", new SqlParameter("@Year", year), new SqlParameter("@Month", month), new SqlParameter("@ProjectID", projectid)).ToList();
    var model = new BugsViewModel
    {
        LoggedBugs = loggedBugs,
        ClosedBugs = closedBug
    };
    return Json(model, JsonRequestBehavior.AllowGet);
}