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

ASP.net MVC4 using pass Model to ObjectDataSource

1 Answer 128 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Moses Fetters
Top achievements
Rank 1
Moses Fetters asked on 08 Jul 2014, 07:40 PM
I've recently began updating one of our projects from Reporting Q1 2013 (7.2.14.127) to Q2 2014 (8.1.14.618), but have ran into the problem of trying to pass a model to an objectdatasource.  Previously, I used 8 Easy Steps to display the ReportViewer using .aspx and would use an InstanceReportSource to render the report:

aspx View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MyProject.Models.MyModel>" %>
 
protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        var instanceReportSource = new InstanceReportSource();
  
        Report report = new Report1(Model);
  
        instanceReportSource.ReportDocument = report;
  
        ReportViewer1.ReportSource = instanceReportSource;
        ReportViewer1.ViewMode = ViewMode.Interactive;
    }

Report class:
using MyProject.Models;
 
namespace Admissions.Reports
{
    using Telerik.Reporting;
  
    public partial class Report1 : Report
    {
        public Report1()
        {
            InitializeComponent();
        }
  
        public Report1(MyModel model) : this()
        {
            DataSource = model;
        }
    }
}

With the new version of Telerik Reporting, InstanceReportSource is obsolete when using the new razor syntax to generate the report so we
have to use UriReportSource or TypeReportSource.
I've tried to use a custom report resolver (example at bottom of OP (Missing Report Name), and was able to pass data in using InstanceReportSource, but I wasn't able to attach any report parameters since the Model is being populated inside of the resolver (which I'd prefer not to do) and the report is created in the resolver and not actually using any parameters from the view.

.cshtml view
@{
    var uriRS = new UriReportSource() { Uri = "1"};
}
@(
Html.TelerikReporting().ReportViewer().Id("reportViewer1")
   .ServiceUrl("/api/reports/")
   .TemplateUrl("/Content/ReportViewer/templates/telerikReportViewerTemplate.html")
        .ReportSource(uriRS)
        .ViewMode(ViewModes.INTERACTIVE)
        .ScaleMode(ScaleModes.SPECIFIC)
        .Scale(1.0)
        .PersistSession(false)
      )

Report Resolver
namespace Admissions.Controllers
{
    public class ReportsController : ReportsControllerBase
    {
        protected override ICache CreateCache()
        {
            return Telerik.Reporting.Services.Engine.CacheFactory.CreateFileCache();
        }
  
        protected override IReportResolver CreateReportResolver()
        {
            return new CustomReportResolver();
        }
    }
  
    public class CustomReportResolver : IReportResolver
    {
        public ReportSource Resolve(string reportId)
        {
            var report = new Report();
  
            report = new Report1(new DashboardViewModel { TotalRolesCount = "Hello Report World" });
  
            var irs = new InstanceReportSource { ReportDocument = report };
  
            return irs;
        }
    }
}

Has anyone found a way to pass the Model directly into the report, (var report = new Report1(Model) ); or a solution that would work equally well?

1 Answer, 1 is accepted

Sort by
0
Stef
Telerik team
answered on 11 Jul 2014, 01:38 PM
Hello Moses,

Please avoid posting the same question multiple times. Thus we can keep a better track of the exchanged information.

If you need further help with the issue let us continue the discussion in your other thread on the same question: ASP.net MVC4 using pass Model to ObjectDataSource.


Thank you for your understanding.

Regards,
Stef
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
General Discussions
Asked by
Moses Fetters
Top achievements
Rank 1
Answers by
Stef
Telerik team
Share this question
or