We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Add datasource into reportviewer under asp.net core 2.2 or above

Hi All:

Can I set viewdata , viewbag / model to reportviewer.datasource  in mvc core ?

Thank you

7 Replies

MS Mahendran Shanmugam Syncfusion Team December 17, 2019 08:18 AM

Hi Michael, 

Yes we can able to pass the datasource from ViewBag in our ASP.NET Core Report Viewer control. Please find the below code snippet for your reference. 
Index.cshtml: 
<bold-report-viewer id="viewer" report-service-url="/api/ReportViewer" dataSources="ViewBag.dataSources" processing-mode="Local" ></bold-report-viewer> 


HomeController.cs: 
public IActionResult Index() 
        { 
            ReportDataSource reportDataSource = new ReportDataSource(); 
            reportDataSource.Name = "StoreSales"; 
            reportDataSource.Value = StoreSales.GetData(); 
            ViewBag.dataSources = new List<ReportDataSource> { reportDataSource }; 
            return View(); 
        } 

Regards, 
Mahendran S. 



MK Michael K December 18, 2019 10:36 AM

Dear Mahendran:

Thansk for your help, that is what I want




SS Swetha Srikumar Syncfusion Team December 19, 2019 12:18 AM

Hi Michael, 
  
Thanks for the update. 
  
Regards, 
Swetha 



MK Michael K December 20, 2019 04:20 AM

Hi Mahendran:

Can I know the know the code in "/api/ReportViewer" ?

Thank you


VS Vinoth Srinivasan Syncfusion Team December 23, 2019 12:07 AM

Hi Michael, 

It is our ReportServiceURL to connect the server-side controller in our client side. For your reference please find the below help documentation. 

Regards, 
Vinoth S. 



MK Michael K December 25, 2019 10:25 PM

Dear Vinoth :

Thanks for your prompt, I have tried to do, but the error. 
Firstly, I try to copy the template code from your web page

Then in the viewer code : I add this

<h1>PreviewReport</h1>

<bold-report-viewer id="viewer" report-path="CurrentListv15.rdl" report-service-url="/api/ReportViewer" dataSources="ViewBag.dataSources" processing-mode="Local"></bold-report-viewer> 

For the Homecontroller, I write this
public IActionResult PreviewReport()
        {
            ReportDataSource reportDataSource = new ReportDataSource();
            reportDataSource.Name = "dtClassRecords";
            using (DBPool = new DBPool())
            {
                reportDataSource.Value = db.dsClassMate.FromSqlRaw("SELECT TOP 500 * FROM [ClassMate]").ToList();
                ViewBag.dataSources = new List<ReportDataSource> { reportDataSource };
                return View();
            }
          
        }

When I run it , the report viewer display "Unable to cast object of type 'System.Text.Json.JsonElement' to type 'System.String'."
Can you advise something ?

Thank you 

 


VS Vinoth Srinivasan Syncfusion Team December 30, 2019 01:45 AM

Hi Michael, 

We have validated the mentioned issue by passing the datasource via ViewBag and it is rendering properly at our end. So, could you please share your issue reproducible application with us to validate the reported problem at our end. 

Regards, 
Vinoth S. 


Loader.
Live Chat Icon For mobile
Up arrow icon