在MVC中使用Viewmodel和datepicker与Dotnet Highchart

时间:2012-05-10 15:29:20

标签: c# asp.net-mvc asp.net-mvc-3 charts highcharts

我正在使用基于C#代码的Dotnet Highchart,它具有输出代码javascript。 Dotnet HighChart

这几乎就是我的图表的样子:

Charts

我使用数据库中的数据通过LINQ查询提供此图表数据。

我的实体中有一个“完成日期”列,我需要使用datepicker制作两个文本框,以便用户可以选择完成日期的开始和结束日期。然后用户应该可以单击提交按钮然后图表应更新并显示两个日期之间的统计信息。我有完整的LINQ查询

这是我要使用的LINQ查询:

public List<CoreValueAndAverageGrade> GetAverageGradeForAllCoreValues(DateTime startDate, DateTime endDate)
        {



            return db.CoreValue
                .Where(coreValue => coreValue.CoreValueQuestion
                    .Any(coreValueQuestion => coreValueQuestion.SubjectType.Ignored_Statistic == false))
                .Select(coreValue => new CoreValueAndAverageGrade
                {
                    CoreValue = coreValue,
                    AverageGrade = coreValue.CoreValueQuestion
                        .Where(coreValueQuestion => coreValueQuestion.SubjectType.Ignored_Statistic == false)
                        .Average(coreValueQuestion => coreValueQuestion.SelectedQuestions
                            .Where(selectedQuestion => selectedQuestion.GoalCardQuestionAnswer != null
                                && selectedQuestion.GoalCardQuestionAnswer.Grade.HasValue

                            && selectedQuestion.GoalCard.Completed_Date >= startDate
                            && selectedQuestion.GoalCard.Completed_Date <= endDate
                                )
                            .Average(selectedQuestion => selectedQuestion.GoalCardQuestionAnswer.Grade.Value))
                })
                .ToList();
        }

这是我的ViewModel

 public class OfficeStatisticNKIViewModel
    {
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
    }

在我的GET操作方法中,我有以下代码:

public ActionResult OfficeStatistic()
{
    {
       OfficeStatisticNKIViewModel model = new OfficeStatisticNKIViewModel();
       model.EndDate = DateTime.Now;
       model.StartDate = DateTime.Now;
        var averageGrades = OfficeStatisticRepository.GetAverageGradeForAllCoreValues(model.StartDate, model.EndDate);

        var dataItems = (averageGrades.Select(averageGrade => averageGrade.AverageGrade).ToArray());
        Data data = new Data(
            dataItems.Select(y => new Point {Color = GetBarColour(y), Y = y}).ToArray());


        Highcharts chart1 = new Highcharts("Chart")
         .SetXAxis(new XAxis { Categories = averageGrades.Select(averageGrade => averageGrade.CoreValue.Name).ToArray() })
         .SetYAxis(new YAxis { Min = 0, Max = 10, TickInterval = 1, Title = new YAxisTitle { Text = "Betygskalan" } })
         .SetSeries(new Series {  Data = data, Name = "Snittbetyg" })
         .SetLegend(new Legend { Enabled = false })
         .SetTitle(new Title { Text = "Örebro Statistik", })
         .InitChart(new Chart { DefaultSeriesType = ChartTypes.Column });


        return View(new Container(new[] { chart1 }));

    }

问题在于我不知道如何使用post action方法,图表需要使用新的统计信息进行更新,我是否需要某种ajax或者我可以在服务器端执行此操作吗?

非常感谢任何形式的帮助或提示!

提前致谢!

1 个答案:

答案 0 :(得分:0)