Reputation: 53
I am using Chart in Web.Helpers to render graphs for a view. The data set for this view can be distinguished by type into subsets, and the view allows the user to select a type.
My razor view containing this graph (_Chart.schtml) is simply:
<p>
<img src="/MyController/MyChart" />
</p>
The controller code (simplified)
public void MyChart()
{
ChartSeries[] data = GetChartData(new string[] { _curChartDataType });
var chart =
new Chart(500, 200, ChartTheme.Green);
for (int i=0; i<data.Length; i++) {
title += data[i].name + " ";
chart.AddSeries(name: data[i].name, chartType: "Line",
xValue: data[i].xValue, xField: data[i].xField,
yValues: data[i].yValues, yFields: data[i].yFields);
}
chart.AddTitle(title)
.AddLegend()
.Write("png");
}
public ActionResult ObjectList()
{
// irrelevant code removed
return PartialView("_Chart");
}
In the parent view's cshtml, I use jquery to catch the user's change in type selection and invoke the call to MyController/ObjectList in ajax.
The graph renders properly when the view is first loaded. However, the user-invoked type changes result in a subsequent call to ObjectList (as expected), but MyChart() never gets called again after the first time.
Anything I should do to ensure MyChart() gets called again whenever PartialView("_Chart") is called? Any suggestions appreciated.
Upvotes: 1
Views: 35
Reputation: 53
Okay, finally on a hunch, I made the following changes:
public WebImage MyChart()
{
// same as before
...
return chart.ToWebImage();
}
And guess what that was all it takes, for MVC's view engine to recognize that the chart object needs updating. The Chart object has a Write() method to write to its own internal structure (which somehow is made available as an image to the html), and ToWebImage() to write explicitly to a structure that the html can use in exactly the same way.. The difference being that the latter one knows to get updated. Go figure.
I hope this helps someone.
Upvotes: 0
Reputation: 1946
Make sure that JQuery isn't caching the results, on your ajax method, put the following:
$.ajax({
cache: false
....
});
Upvotes: 0