使用局部视图作为图像src时无法使用剃刀构建图表 [英] Can't build a chart with razor when using partial view as image src

查看:76
本文介绍了使用局部视图作为图像src时无法使用剃刀构建图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用剃刀制作图表? 尝试使用部分视图( _Chart.cshtml ):

How can I make chart in view with razor? Tried to use partial view (_Chart.cshtml):

@{
    var usdChart = new Chart(width: 600, height: 400)
    .AddTitle("Заголовок")
    .AddSeries(
        name: "USD",
        xValue: new[] { "1", "2", "3", "4" },
        yValues: new[] { "11", "22", "33", "44" })
    .Write();
}

而且在视野范围内:

<img src="@Html.Partial("_Chart")"/>

但是它不起作用.

推荐答案

使用当前代码,它将不会在主视图中呈现图像.相反,它仅渲染图像.这是因为调用局部视图时,Chart.Write方法会将图表对象转换为jpg并写入输出流.

With your current code, it will not render the image in the main view. Instead it render just the image. It is because when you make the partial view call, the Chart.Write method will convert the chart object to a jpg and write to the output stream.

您应该创建一个操作方法,该方法将返回部分视图结果并将其用作图像src属性值

You should create an action method which returns this partial view result and use that as the image src attribute value

public ActionResult Chart()
{
    return PartialView("_Chart");
}

并在主视图中

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

页面加载后,它将对图像源URL进行单独的http调用,该URL仅返回图像

When the page loads, it will make a separate http call to the image source url which returns just the image

这篇关于使用局部视图作为图像src时无法使用剃刀构建图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆