MVC Charts - Passing data from one view to another cshtml

Working in an existing MVC application I am creating various charts to display customer data. I am completely new to charts in ASP.NET. I mainly use http://www.asp.net/web-pages/overview/data/7-displaying-data-in-a-chart as a resource for making charts.

I am currently creating charts in order and usually my x values ​​are fixed (like Jan, Feb, Mar, Apr, etc.) and my y values ​​are found by querying a list that is sent to the view.

The chart objects I create however create an image which is the only thing displayed on the page and my layout is lost as a result, obviously.

The article mentions embedding in a web page by calling the chart from a separate view, for example:

  <body>
    <p><img src="ChartArrayBasic.cshtml" /> </p>
  </body>

      

Since I am creating my charts by asking for a list in the view for variable values, to make them more general chart views that can be used by all clients for their own data, I need a .cshtml file that I call to get the list.

Is it possible to: 1. Call the view from the action method. 2. The view displays a regular page with another view embedded in it. 3. the inline view must receive a List object from the original view.

Thanks, JK

+3


source to share


1 answer


@JonnyKnottsvill. You basically do what was > here , except as above to create the PartialView so you can reuse this later as you want.

_ChartParialView.cshtml

in the General folder inside Views :

@model IEnumerable<string>

@{
    var myChart = new Chart(width: 600, height: 400)
        .AddTitle("Chart Title")
        .AddSeries(
            name: "Employee",
            xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" },
            yValues: Model)
        .Write();
}

      

Controller:



public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult DrawChart()
    {
        var model = new[] { "2", "6", "4", "5", "3" };

        return PartialView("_ChartPartialView", model);
    }
}

      

The Home (Index) view where you want the graph to be displayed as an image:

@{
    ViewBag.Title = "Chart";
}

<h2>Chart</h2>

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

      

+2


source







All Articles