c# - 使用自定义 TextWriter 时 OutputStream 不可用 mvc

标签 c# asp.net-mvc charts

我试图在我的 View 中获取图表,并在我的 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>

这一行 <% myChart.Controls.Add(ViewData["Chart"] as Chart); %> 使用自定义 TextWriter 时生成错误消息 OutputStream 不可用

这是来自 Controller 的代码:

    public ActionResult ChartResult()
    {

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

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

        return View();
    }

CreateChart 函数只是创建一个以 Column 作为图表类型的图表。 为什么会出现此错误,使用自定义 TextWriter 时 OutputStream 不可用?

最佳答案

我认为问题出在错误的方法上。 构建图表时,您有两种选择。

一种方法是生成图表图像,然后您的 View 将包含<img />。用 src 标记属性设置为生成图表图像的 Controller /操作的 url。

另一种方法是使用 JSON 操作构建 Controller ,它返回图表仅数据。这是一种更灵活的方式,因为您可以自由选择任何现成的可视化图表(html5 canvas 或 svg)的 javascript 库

关于c# - 使用自定义 TextWriter 时 OutputStream 不可用 mvc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6456195/

相关文章:

c# - 在 C# 中每行读取一个字符串行

c# - 为一个 Controller 创建多个路由

google-sheets - 对饼图的切片进行排序,而不对列中的数据进行排序

wpf - 在WPF工具包图表中更改绘图区域和标题周围的边距

c# - 将项目上传到开源存储库时,是否应该删除 bin 和 obj 文件夹?

c# - C# 中的无限数字

c# - 是否有一个存储库可以抽象出 Azure、S3 等?

asp.net-mvc - MVC 4 中的自定义路由

c# - MVC4 中未在 DropDownListFor() 上设置所选值

c# - 如何在 C# 中的柱形图顶部添加百分比