开发者

MVC 3 Displaying a dynamic image created in a controller

I'm trying generate a images/chart in my controller and have that image displayed in an image tag in the view. In my view, I have

<div id="graphcontainer"><a href="#">Close Graph</a><img id="chartImage" alt="" /></div>

and in my controller (called EventReport) I have a method called "BuildChart". My idea was to capture the click event of a button designated as the build report button. In the click event handler I would want to assign the image's source to something like "/EventReport/BuildChart" to have the image control populated.

Here's what's in the controller

public ActionResult BuildChart()
{
    var chart = new Chart
                    {
                        Height = Unit.Pix开发者_开发技巧el(400),
                        Width = Unit.Pixel(600),
                        AntiAliasing = AntiAliasingStyles.Graphics,
                        BackColor = Color.White
                    };

    // Populate chart here ... 

    var ms = new MemoryStream();
    chart.SaveImage(ms);
    return File(ms.ToArray(), "image/png");
}

I'm just having problems wiring this up. Am I on the right track?

Thanks!


Set src of your image as link to your action on the click of your button, eg:

using jquery:

$('#build-chart').click(function() {
   $('#chartImage').attr('src', '/EventReport/BuildChart');
});

and action will be asked for content for image. Probably some 'loading' indicator will be needed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜