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

how to use objectdatasource with report file in .NET MVC

3 Answers 583 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
quang
Top achievements
Rank 1
quang asked on 19 May 2015, 08:03 AM

Dear Telerik,

we are trying to use a report file Item.trdx with ObjectDataSource in .NET MVC but the view can not render my report.

1) In ReportDesigner

I created a file Item.trdx, then tried to link to a fake sqlDataSource to test in preview mode all good.

2) ObjectDataSource: I have below classes in Model (I may build these classes into class library .dll later if required)

public class
ItemLocationModel
{
        public string ITEM_NO { get; set; }
        public string PREV_LOC { get; set;}       
}

public class ItemLocation
{
       [DataObjectMethod(DataObjectMethodType.Select)]
        public static IList<ItemLocationModel> GetItemLocation(string item_no)
        {
            List<ItemLocationModel> l =new List<ItemLocationModel>();           

            //fill up data here based on item_no....

            return l;
        }
}

3) In Controller: I am trying to use Resolver

class
CustomReportResolver : Telerik.Reporting.Services.Engine.IReportResolver
{
        public Telerik.Reporting.ReportSource Resolve(string item_no)
        {
Telerik.Reporting.ObjectDataSource objectDataSource = new Telerik.Reporting.ObjectDataSource();
objectDataSource.DataSource = ItemLocation.GetItemLocation(item_no);
Telerik.Reporting.Report report = new Telerik.Reporting.Report();
report.DataSource = objectDataSource;
Telerik.Reporting.InstanceReportSource reportSource = new Telerik.Reporting.InstanceReportSource();
reportSource.ReportDocument = report;           
return reportSource;
}

public class ReportsController : Telerik.Reporting.Services.WebApi.ReportsControllerBase
{
        protected override Telerik.Reporting.Services.Engine.IReportResolver CreateReportResolver()
        {
            return new CustomReportResolver();
        }
        protected override Telerik.Reporting.Cache.Interfaces.ICache CreateCache()
        {
            return Telerik.Reporting.Services.Engine.CacheFactory.CreateFileCache();
        }
}

 

4) In CSHTML: i am populating the ReportViewer

<div id="reportViewer1" class="k-widget">
            loading...
</div>
    <script type="text/javascript">
        $("#reportViewer1")
            .telerik_ReportViewer({
                serviceUrl: "/api/reports/",
                templateUrl: '/ReportViewer/templates/telerikReportViewerTemplate-9.0.15.324.html',
                reportSource: {
                    report:  1
                }
            });
</script>

 

5) RESULT:

I debugged the Resolve() function and it clearly got data correctly, but it does not show my my report, it totally empty without any error even the template was loaded. My questions are:

- How to load data returned back from an InstantDataSource in CSHTML file?

- How to bind my returned data with a report file .trdx?

 

Thanks so much in advanced.

Quang

3 Answers, 1 is accepted

Sort by
0
Nasko
Telerik team
answered on 21 May 2015, 01:41 PM
Hello Quang,

The following line from your code:
Telerik.Reporting.Report report = new Telerik.Reporting.Report();

creates an empty report object. This report does not contain any sections or any items. It is an empty report, which will be blank when displayed inside a report viewer.

In order to display your report, you need to work with the Item.trdx file, deserialize it to a report object, as explained in the Serializing Report Definition in XML help article, and then modify its data source to reference the new ObjectDataSource component.

Regards,
Nasko
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
仁劭
Top achievements
Rank 1
answered on 08 Jun 2018, 05:43 AM

In this case If I don't want to use item.trdx 
I want to use the ReportLibrary the report1.cs
How can I Pass the object source from the web site 

In the picture , I Create a class like UserInfo 

And I create a method named GetUserInf()
I can build and get answert by the picture but now If  passing a string It will be not work 
can I use the reportController Passing Object to the Report1.cs ?

0
Todor
Telerik team
answered on 12 Jun 2018, 02:11 PM
Hi 仁劭,

You can instantiate the report in the Resolve() method of the CustomReportResolver, assign its DataSource and return it wrapped in an InstanceReportSource, i.e.

public Telerik.Reporting.ReportSource Resolve(string reportId)
{
    Telerik.Reporting.ObjectDataSource objectDataSource = new Telerik.Reporting.ObjectDataSource();
    objectDataSource.DataSource = GetUserInfo();
 
    Report1 report = new Report1();
    report.DataSource = objectDataSource;// you can use also GetUserInfo() directly since it is a List
     
    Telerik.Reporting.InstanceReportSource reportSource = new Telerik.Reporting.InstanceReportSource();
    reportSource.ReportDocument = report;
               
    return reportSource;
}

Note that the string passed to the Resolve() method (i.e. reportId in the above code snippet) is intended to be used to identify the report. If necessary other information can also be included there. Custom logic can be used to split the information carried by reportId and used to identify the report, data source, etc.

The place where the report can be modified (i.e. the information related to the report definition can be changed) after being passed from the Html5 Report Viewer is the Resolve() method of the Report Resolver of the Telerik Reporting REST Service. The method must return a valid server side ReportSource that will be used by the Reporting Engine.

Regards,
Todor
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
quang
Top achievements
Rank 1
Answers by
Nasko
Telerik team
仁劭
Top achievements
Rank 1
Todor
Telerik team
Share this question
or