Can't build a chart with razor when using partial view as image src

好久不见. 提交于 2019-12-20 07:26:27

问题


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();
}

And in view:

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

But it don't work.


回答1:


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.

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");
}

and in the main view

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

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



来源:https://stackoverflow.com/questions/46203840/cant-build-a-chart-with-razor-when-using-partial-view-as-image-src

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!