How to use ReportBook with asp.net mvc 5 ?

5 posts, 1 answers
  1. Laurent
    Laurent avatar
    11 posts
    Member since:
    Mar 2014

    Posted 21 Apr 2015 Link to this post

    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 ?

  2. Answer
    Nasko
    Admin
    Nasko avatar
    1045 posts

    Posted 22 Apr 2015 Link to this post

    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.

     
  3. Laurent
    Laurent avatar
    11 posts
    Member since:
    Mar 2014

    Posted 04 May 2015 Link to this post

     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

  4. Laurent
    Laurent avatar
    11 posts
    Member since:
    Mar 2014

    Posted 04 May 2015 in reply to Laurent Link to this post

     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.

     

  5. Nasko
    Admin
    Nasko avatar
    1045 posts

    Posted 05 May 2015 Link to this post

    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.

     
Back to Top