7

Im trying to get a chart on my view and I´m displaying it like this in my view:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>ChartResult</h2>

<% using (Html.BeginForm("HandleChartType", "Chart")) %>
<% { %>

    <%= Html.DropDownList("ListItems", "Select Chart Type")%>

    <input type="submit" value="Set Chart" />
 <%} %>




<% myChart.Controls.Add(ViewData["Chart"] as Chart);  %>  
<asp:Panel ID="myChart" runat="server"></asp:Panel>



<!--<img src="/Chart/CreateChart" alt="" />-->


<h2>FormResults</h2>

</asp:Content>

This line <% myChart.Controls.Add(ViewData["Chart"] as Chart); %> generates the error message OutputStream is not available when a custom TextWriter is used

This is the code from the controller:

    public ActionResult ChartResult()
    {

        List<string> items = GetFilteredChartTypes();
        ViewData["ListItems"] = new SelectList(items);

        Chart myChart = CreateChart(SeriesChartType.Column);
        ViewData["Chart"] = myChart;

        return View();
    }

The CreateChart function just creates a chart with Column as chart type. Why do I get this error, OutputStream is not available when a custom TextWriter is used?

casperOne
  • 73,706
  • 19
  • 184
  • 253
user228720
  • 103
  • 4
  • 9
  • possible duplicate of [BinaryWrite exception "OutputStream is not available when a custom TextWriter is used" in MVC 2 ASP.NET 4](http://stackoverflow.com/questions/2261198/binarywrite-exception-outputstream-is-not-available-when-a-custom-textwriter-is) – stuartd Jun 23 '11 at 15:05
  • Why you use MVC view like a WebForms page? – ilyabreev Mar 09 '14 at 14:55

1 Answers1

0

I think that problem is in wrong approach. When building charts you have two options.

One way is to generate chart image and then your view will consist of <img /> tag with src attribute set to url of controller/action which generates chart image.

Another way is to build controller with JSON action, which returns chart data only. This is more flexible way because you'll get freedom to choose any of ready-to-go javascript libraries that visualize charts (html5 canvas or svg)

ilyabreev
  • 628
  • 6
  • 20