如何在MVC5中使用_Layout显示图表?

时间:2015-10-09 17:33:53

标签: asp.net-mvc visual-studio charts asp.net-mvc-5 visual-studio-2015

我有一个StepTwo视图,它将稍后将使用的一些信息传递给以下ActionResult:

[HttpPost]
public ActionResult CreatedIndicator(string seriesSelect)
{
    wwStartSelected = (int)TempData["wwStartSelected"];
    wwEndSelected = (int)TempData["wwEndSelected"];
    chartTypeSelected = (string)TempData["chartTypeSelected"];

    var testChart = new Chart(width: 600, height: 400)
        .AddTitle("Test")
        .AddSeries(
            name: "Employee",
            xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" },
            yValues: new[] { "2", "6", "4", "5", "3" }).Write();

    return View(testChart);
}

然后将图表传递到相应的视图:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@{
    ViewBag.Title = "CreatedIndicator";
}

<h2>Created Indicator</h2>

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

但是,结果视图仅显示图表;视图不显示带有标准页眉/页脚的_Layout。如何修复它,以便除了图表之外还显示_Layout?

编辑:我还能够通过创建新的MVC模板项目并设置以下内容来复制该问题:

HomeController中:

public ActionResult About()
{
    ViewBag.Message = "Your application description page.";

    var testChart = new Chart(width: 600, height: 400)
        .AddTitle("Test")
        .AddSeries(
            name: "Employee",
            xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" },
            yValues: new[] { "2", "6", "4", "5", "3" }).Write();

    return View(testChart);
}

关于观点:

@{
    ViewBag.Title = "About";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

<p>Use this area to provide additional information.</p>

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

编辑2:使用MVC模板时的图像澄清(注意缺少段落文本&amp;&amp;模板页眉/页脚) enter image description here

1 个答案:

答案 0 :(得分:2)

这就是图表助手不幸的工作方式..

如果你想要显示图表助手正在创建的图像..你应该只为想要显示它的视图添加<img>并有一个单独的fileresult为你返回图像数据

将About.cshtml视图更改为

@{
    ViewBag.Title = "About";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

<p>Use this area to provide additional information.</p>

<img src="@Url.Action("MyChart")" alt="SimpleChart" />

并将此操作添加到HomeController。

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

public ActionResult MyChart()
{
    var testChart = new Chart(width: 600, height: 400)
        .AddTitle("Test")
        .AddSeries(
            name: "Employee",
            xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" },
            yValues: new[] { "2", "6", "4", "5", "3" })
        .GetBytes("png");
    return File(testChart, "image/png");
}