This is a migrated thread and some comments may be shown as answers.

How to use ReportBook with asp.net mvc 5 ?

4 Answers 238 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Laurent
Top achievements
Rank 1
Laurent asked on 21 Apr 2015, 07:46 AM

Hi,

For an app i'm developing, the user can print several invoices selected in a list.

 The invoice report works just fine for one item. I decided to reuse this code and include them in a ReportBook object. What I can't figure is how to send the ReportBook object to the view with a ReportViewerViewModel object.

 public ActionResult Invoices(IEnumerable<int> ids)
{

     var book = new ReportBook();
     ids.ForEach(id =>
      {
           var invoiceReport = new ReportDefinitions.Invoice();
           book.Reports.Add(invoiceReport);

      });           
     var source = new InstanceReportSource {ReportDocument = book};
     var viewer = new ReportViewerViewModel();

 

     return ?
}

 If my way of doing is right, how can I pass the book to the Index view which use a ReportViewerViewModel model or resolve the report ?

4 Answers, 1 is accepted

Sort by
0
Accepted
Nasko
Telerik team
answered on 22 Apr 2015, 02:38 PM
Hello Laurent,

The default implementation of the reports controller can resolve a string which contains either the assembly-qualified name of the report, or the path to a TRDX file.
In order to pass a report book object wrapped in an InstanceReportSource you need to use a custom report resolver e.g:
protected override IReportResolver CreateReportResolver()
{
    return new CustomReportResolver();
}
 
class CustomReportResolver : Telerik.Reporting.Services.Engine.IReportResolver
{
    public Telerik.Reporting.ReportSource Resolve(string reportName)
    {
        var report = Activator.CreateInstance(Type.GetType("CSharp.MvcDemo." + reportName + ", CSharp.MvcDemo")) as Telerik.Reporting.Report;
        var reportBook = new Telerik.Reporting.ReportBook();
        reportBook.Reports.Add(report);
        ...
         
        var reportSource = new Telerik.Reporting.InstanceReportSource();
        reportSource.ReportDocument = reportBook;
 
        return reportSource;
    }
}

More information on the topic can be found in the REST Service Report Resolver help article.

Regards,
Nasko
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Laurent
Top achievements
Rank 1
answered on 04 May 2015, 03:15 PM

 Hi Nasko,

 Thanks for you answer. I follow your instructions. Yet, my code does not work properly.

 I created the method bellow in the CustomReportResolver. The program iterates and add an invoice for each id in the request. But when the report appears on screen, I have the same invoice repeated instead of different ones. Also the PageFooter is truncated.

01.private ReportSource ResolveInvoices(string ids)
02.{
03.        var book = new ReportBook();
04.        ids.Replace("[Invoices]", "").Replace("\"", "")
05.            .Split(',').ForEach(item =>
06.        {
07.            int id;
08.            if (!int.TryParse(item, out id)) { return; }
09. 
10.            var report = Activator.CreateInstance(typeof(ReportDefinitions.Invoice)) as Report;
11.            Debug.Assert(report != null);
12.            report.ReportParameters["InvoiceId"].Value = "=" + id;
13.            book.Reports.Add(report);
14.        });
15. 
16.        return new InstanceReportSource
17.        {
18.            ReportDocument = book
19.        };
20.}

 

Laurent

0
Laurent
Top achievements
Rank 1
answered on 04 May 2015, 08:48 PM

 Got it. Before adding each parameter, I added this line to prevent parameter merging to the first value. It worked.

1.report.ReportParameters["InvoiceId"].Mergeable = false;

 Still. I don't have the report footers.

 

0
Nasko
Telerik team
answered on 05 May 2015, 11:05 AM
Hello Laurent,

We are not sure what is the exact reason for the missing/truncated Report Footer or Page Footer. Our report book demo does not exhibit the behavior, so it will be greatly appreciated if you elaborate further on the issue by opening a new support ticket and providing us a sample project exhibiting the described behavior.

Regards,
Nasko
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
General Discussions
Asked by
Laurent
Top achievements
Rank 1
Answers by
Nasko
Telerik team
Laurent
Top achievements
Rank 1
Share this question
or