开发者

ASP.NET MVC : RenderAction for action writing image throws exception

开发者 https://www.devze.com 2023-02-12 07:42 出处:网络
I have an action returning an image: public void SensorData(int id, int width = 300, int height = 100)

I have an action returning an image:

    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();
        }
    }

This action renders fine when i call it from the browser (ex. controller_name/SensorData/6). The problem is that when i try to view it using Html.RenderAction i get the following compilation exception on my view:

The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments.

This is the code generating the exception:

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

Any ideas on what m开发者_如何学Cight be causing this?

Thanks


You have two possibilities:

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

or:

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

Contrast this with their equivalents using the WebForms view engine:

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


You can't embed the image data directly in the HTML. Try using an image tag and pointing to the action instead.

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

精彩评论

暂无评论...
验证码 换一张
取 消