如何在MVC3中选择了部分视图上的过滤器后,才能在视图中呈现图形

时间:2012-10-24 16:02:33

标签: c# asp.net-mvc-3 razor graph partial-views

我在 C# Razor 中有 ASP.NET MVC3 应用程序。

我想创建一个包含过滤器的页面(例如年,月),一旦用户选择过滤器并单击提交按钮,就会显示一个图表。我使用以下代码(不工作): 查看 Stats.cshtml

@{
   ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>

@{ Html.RenderPartial("StatsFilter", Model.FilterViewModel);  }

<img src="@Url.Action("DrawChart")" alt="Drawing chart with HTML Helper" />

控制器

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View("Stats", StatsViewModel);
        }

        [HttpPost]
        public ActionResult Index(FiltersViewModel filters)
        {
            //Retrieve Data
            return View("Stats", StatsViewModel);
        }

        public ActionResult DrawChart(GraphViewModel graph)
        {
            var chart = new Chart(width: 500, height: 600)
                .AddTitle("Chart Title")
                .AddSeries(
                            chartType: "Line",
                            name: "Stats",
                            xValue: chartData.XValues.Keys.ToList(),
                            yValues: chartData.XValues.Values.ToList())
                .GetBytes("png");
            return File(chart, "image/bytes");
        }
    }

视图模型

public class StatsViewModel
{
    public FilterViewModel Filters { get; set; }
    public GraphViewModel Graph { get; set; }
}

使用这样的解决方案,我不知道如何将ViewModel传递给图表。此外,如果我进行一些小的调整(例如从GraphViewModel操作方法中移除DrawChart),则无论如何都会显示图表,即使没有数据也是如此。

我想让用户选择过滤器,然后显示图表。我希望我走的是正确的道路,我的代码不会被彻底抛弃。

1 个答案:

答案 0 :(得分:0)

您的GraphViewModel graph未在控制器的任何位置使用。所以你不需要这个参数。

但是如果你需要将一些vales传递给你的控制器,那么你可以使用url中的查询字符串来完成它。