Mapping System.Web.Helpers.Chart in a partial view from the model

So, I tested the chart helpers in the System.Web.Helpers namespace.

according to http://www.asp.net/web-pages/tutorials/data/7-displaying-data-in-a-chart

I am making a diagram in the form of .cshtml, but instead I want to save it in the ViewModel.

No problem, except when I try to display it as a smaller image on a website.

I thought the cleanest solution would be to create one common partial view for rendering graphs from models

_graph.cshtml

@model System.Web.Helpers.Chart @Model.Write() 

And then do this partial presentation in some way on the respective websites. I tried several versions, but cannot make it work.

Website.cshtml

 <div> <h2>Some header above a graph</h2> <img src="@Html.Partial("_graph", Model.TheChart)" /> </div> 

This does not work, and I'm not sure how to do it. I think that now I can think that all models with charts inherit the interface that Chart provides, and let it be the model for _graph.cshtml.

 <img src="_graph.cshtml" /> 

But not sure if this is using a model.

Any opinions?

+4
source share
2 answers
 <div> <h2>Some header above a graph</h2> <img src="@Url.Action("DrawChart")" /> </div> 

and then you can have a controller action:

 public ActionResult DrawChart() { MyViewModel model = ... return View(model); } 

and the corresponding view that will draw the chart ( DrawChart.cshtml ):

 @model MyViewModel @{ // TODO: use the data from the model to draw a chart var myChart = new Chart(width: 600, height: 400) .AddTitle("Chart Title") .AddSeries( name: "Employee", xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" }, yValues: new[] { "2", "6", "4", "5", "3" }) .Write(); } 

and the result obtained:


enter image description here

+11
source
 <div> <h2>Some header above a graph</h2> <img src="@Url.Action("DrawChart")" /> </div> public ActionResult DrawChart() { MyViewModel model = ... return View(model); } 

!!! To send the Model parameter to DrawChart

Change to

 <div> <h2>Some header above a graph</h2> <img src="@Url.Action("DrawChart",Model)" /> </div> public ActionResult DrawChart(MyViewModel _MyViewModel ) { MyViewModel model = MyViewModel ; return View(model); } 

MyViewModel - Null

Seek advice from those who know.

0
source

Source: https://habr.com/ru/post/1401179/


All Articles