【问题标题】:MVC Charts - Passing data from one view to another cshtmlMVC 图表 - 将数据从一个视图传递到另一个 cshtml
【发布时间】:2015-05-02 02:20:26
【问题描述】:

在现有的 MVC 应用程序中工作,我正在创建各种图表来显示客户端数据。我对 ASP.NET 中的图表完全陌生。我主要使用http://www.asp.net/web-pages/overview/data/7-displaying-data-in-a-chart 作为创建图表的资源。

目前我正在很好地创建图表,通常我的 x 值是固定的(例如一月、二月、三月、四月等),我的 y 值是通过查询发送到视图的列表来找到的。

我正在创建的图表对象创建了一个图像,然后这是页面上显示的唯一内容,结果我的布局显然丢失了。

文章提到通过从单独的视图调用图表来嵌入网页:

  <body>
    <p><img src="ChartArrayBasic.cshtml" /> </p>
  </body>

当我通过在视图中查询变量值的列表来生成图表时,为了使它们成为更通用的图表视图,可供所有客户端用于他们自己的数据,我需要我调用的 .cshtml 文件接收列表。

是否可以: 1.从action方法调用视图。 2. 该视图显示一个常规页面,其中嵌入了另一个视图。 3. 嵌入视图需要从初始视图接收一个List对象。

谢谢, JK

【问题讨论】:

  • 创建一个返回图表部分视图的ChildOnlyAction,然后使用@Html.Action()在视图中呈现它

标签: asp.net-mvc charts


【解决方案1】:

@JonnyKnottsvill 您基本上按照answered here 的做法进行操作,但如上所述,创建一个 PartialView 以便您以后可以根据需要重复使用它。

_ChartParialView.cshtmlViews 内的 Shared 文件夹中:

@model IEnumerable<string>

@{
    var myChart = new Chart(width: 600, height: 400)
        .AddTitle("Chart Title")
        .AddSeries(
            name: "Employee",
            xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" },
            yValues: Model)
        .Write();
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult DrawChart()
    {
        var model = new[] { "2", "6", "4", "5", "3" };

        return PartialView("_ChartPartialView", model);
    }
}

您希望将图表呈现为图像的主页(索引)视图:

@{
    ViewBag.Title = "Chart";
}

<h2>Chart</h2>

<img src="@Url.Action("DrawChart")" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    相关资源
    最近更新 更多