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

前端 未结 1 343
無奈伤痛
無奈伤痛 2021-01-26 14:54

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

@{
    var usdChart = new Chart(width: 600, height: 400)
    .AddTit         


        
相关标签:
1条回答
  • 2021-01-26 15:29

    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

    0 讨论(0)
提交回复
热议问题