views:

3698

answers:

5

How do I integrate reportviewer in asp.net mvc project?

I want to add business objects of MVCProject.Model namespace.

Reportviewer allows Business objects of DataSet.

Is it possible to choose other data source? Like LINQ data source... or Direct object to LINQ-to-SQL class objects.

What would be the best solution to add reports in MVC project?

+1  A: 

It's gonna be tough. First, you need ViewState so you'll need to host the report in a regular WebForms page. This isn't too bad though - WebForms and MVC work fine side-by-side.

The hard part is binding to real IEnumerable objects and not those phoney-baloney ObjectDataSources.

The first step is to build up a report data model. You can do this in code, with queries, whatever, however you want. A structure something like this (but obviously much bigger) is typical:

public class ReportSource
{   
   public Floogle[] Floogles { get; set; }
}

public class Floogle
{
    public Doodad[] Doodads { get; set; } 
    public string Text { get; set; }
}

public class Doodad
{
    public int Number { get; set; }
}

The trick is to use a BindingSource control in your report and set the DataSource property to typeof(ReportSource) - yes, the data source is the type of your report model.

When designing your report you won't get a lot of richness, but you'll be able to do it.

As far as third party reporting solutions go, we've found Telerik's to be the best option.

Matt Hinze
Where exactly do I use this BindingSource??
AndreMiranda
First of all, I'm sorry you have to work with this horrible tool. The BindingSource is a "control" you can "drag" to the designer surface.
Matt Hinze
MVC is not designed for drag-and-drop bindable controls. Sure you can 'hack' them into work with MVC (as MVC is still build on the same base as webforms) but it goes against the MVC paradigm so it's not recommended. Shiraz's answer is good if you have existing reports that you must use with MVC.
cottsak
yeah.. i know. we've moved completely away from tools like this btw. in favor of a tool that converts html to pdf (using the CSS3 stuff for page headers, etc)
Matt Hinze
+2  A: 

An alternative way to do this would be to generate the report on the reporting server, then stream it to the mvc app as a PDF.

Shiraz Bhaiji
A: 

I've got a small project I threw up on codeplex that is an mvc project with a report.

http://mvctaskmanagement.codeplex.com/

Basically since I do dev on an XP box, my web form had to get pushed to a separate project. Since I have a service layer proj, I stuck it in there.

From there I call my report via a ajax post shooting the params over to the report page, which then passes it down to the same service layer used to generate the preview.

Good luck!

James Fleming
+2  A: 

I got an idea that is not tested but may work. 1- Place report viewer control in a standard ASP.Net web form page (e.g. ReportViewer.aspx) 2- Under your MVC, add an iframe that references to this ReportViewer.aspx page 3- Pass parameters to the page using sessions or query strings

Let me know if th is works

Hesham Desouky
A: 

@Hesham, This has worked out pretty well for me. Thanks for the suggestion.

Tebogo