Rendering Reports using ASP.NET MVC

21 October 2010

MacgyverAlthough there is no ASP.NET report viewer per se, as MacGyver would instruct, nothing should stop us from strapping a rocket on our shoulders and retrofitting it to suit our own personal needs: making our own report preview window for ASP.NET MVC.

When thinking about ASP.NET MVC, the best way to visualize things is to imagine that each Controller Action method is some kind of URL that returns something. Let’s take the bomb apart (because ASP.NET MVC is the bomb) and see if we can retro-fit an action method to return a rendered report.

This is easily done my friends. There is literally only a few lines of code to achieve this, and a little html to get it displayed.

My final result is shown below:

IFrameRendered

The key here is to use the ContentResult descendant of ActionResult:

 

  1: // since this is in the HomeController its url is:
  2: // ~/Home/ProductReport
  3: public ActionResult ProductReport()
  4: {
  5:     // Load the pre-fab'ed report
  6:     ProductReport report = new ProductReport();
  7:     report.CreateDocument();
  8: 
  9:     var options = new HtmlExportOptions();
 10: 
 11:     // tricky part - export the html into a memory stream
 12:     // but we are pretending to be McG
 13:     using (MemoryStream stream = new MemoryStream())
 14:     {
 15:         report.ExportToHtml(stream, options);
 16:         // move the stream to the beggining
 17:         stream.Seek(0, SeekOrigin.Begin);
 18:         // create a stream reader
 19:         StreamReader reader = new StreamReader(stream);
 20:         // get the actual html
 21:         string html = reader.ReadToEnd();
 22:                 
 23:         // return the actual html
 24:         return new ContentResult
 25:         {
 26:             ContentType = "text/html",
 27:             Content = html
 28:         };
 29:     }
 30: }
The method used to generate the report html is ExportToHtml() that is available on all reports that inherit from XtraReport. The rest is literally playing around with IO.

To get the IFrame to render the report, we need only introduce the following markup:

  1: <iframe id="report" title="Product Report" 
  2:     src="/Home/ProductReport" height="600" width="800"></iframe>

That is it! As always, if there are any questions and/or comments, feel free to get a hold of me!

Seth Juarez
Email: sethj@devexpress.com
Twitter: @SethJuarez

7 comment(s)
Vest (DevExpress Support)

Hello Seth,

Thank you for the article. There is a small report in our Support Center regarding your article: Q292060 (www.devexpress.dev/.../Q292060.aspx). It has a working example which might be helpful to everybody.

Thanks,

Vest

4 November, 2010
Serge Wautier

Vest, your link is broken. Can you please fix it. TIA.

8 December, 2010
Seth Juarez (DevExpress)

Thanks for following up! The proper link is: www.devexpress.com/.../Q292060.aspx. I also have the sample code attached now as well. Thanks for your patience!

8 December, 2010
Alan Erdman

Hello Seth,

Thank you for providing the info, I have incorporated it into my project and am having a problem I hope you may have some insight into. I apologize for not providing sample code, but this is part of a large working project.

The report populates and displays, but then another action that doesn't fire the OnAuthorization event of the AuthorizeAttribute class runs, which causes an exception later in the app.

I realize you didn't get much here, but I'm hoping this is a fairly common problem.

Thanks

7 April, 2011
Seth Juarez (DevExpress)

Alan:

  I am not sure I entirely understand what is going on! I would love to help: shoot me an email (sethj@devexpress.com) to see if we can work it out. As an aside, we are going to be providing better ASP.NET MVC reports out of the box in 11.1. Look for some upcoming posts with details!

7 April, 2011
Hans Skytte

Hey Seth,

I have followed your example in this page and it all works well. Now i need to bind the xrLabels to some objects in my viewmodel, but I can't create a datasource for my report.

How can I do this? or how do I databind the labels to the viewmodels?

I hope you can help me out :).

Thanks in advance,

Hans Skytte

4 May, 2011
Joshua Sigar

What to do if I want to display the toolbar as well?

2 June, 2011

Please login or register to post comments.