【问题标题】:ASP.NET MVC : RenderAction for action writing image throws exceptionASP.NET MVC:用于操作写入图像的 RenderAction 引发异常
【发布时间】:2011-02-22 11:04:28
【问题描述】:

我有一个返回图像的操作:

    public void SensorData(int id, int width = 300, int height = 100)
    {
        using (var repo = new DataRepository())
        {
            var sensor = repo.GetSensor(id);
            if (sensor == null) return;

            var chart = new Chart(width, height);
            chart.AddSeries(name: "Values", 
                            chartType: "line", 
                            xValue: sensor.DataValues.Select(s => s.Date).ToList(),
                            yValues: sensor.DataValues.Select(s => s.Value).ToList());

            chart.Write();
        }
    }

当我从浏览器调用此操作时(例如,controller_name/SensorData/6),该操作呈现正常。问题是,当我尝试使用 Html.RenderAction 查看它时,我的视图中出现以下编译异常:

'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' 的最佳重载方法匹配有一些无效参数。

这是产生异常的代码:

@Html.RenderAction("SensorTypes", new { id = 6});

有什么可能导致这种情况的想法吗?

谢谢

【问题讨论】:

  • 为什么需要这样做?这会不会把图片数据写入网页,想必不是你想要的吧?
  • 这是我发现的 MVC 内 Charts 上的大多数示例的编写方式

标签: asp.net-mvc asp.net-mvc-3 razor


【解决方案1】:

你有两种可能:

@{Html.RenderAction("SensorTypes", new { id = 6 });}

或:

@Html.Action("SensorTypes", new { id = 6 })

将此与使用 WebForms 视图引擎的等效项进行对比:

<% Html.RenderAction("SensorTypes", new { id = 6 }); %>
<%= Html.Action("SensorTypes", new { id = 6 }) %>

【讨论】:

    【解决方案2】:

    您不能将图像数据直接嵌入 HTML。 尝试使用图像标签并指向动作。

    <img src='@Url.Action("SensorTypes", new { id = 6})'  />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 2010-10-11
      • 2017-03-26
      相关资源
      最近更新 更多