Articles in this section
Category / Section

How to pass a report parameter value from client to server in ASP.NET Core ReportViewer?

2 mins read

You can pass report parameter values from client-side to server-side using Syncfusion ASP.NET

 ASP.NET Core ReportView. The following steps guide you to send the parameters from client to server using the ‘ajax-before-load’ event.

1.Create a simple ReportViewer application with the help of getting started documentation.

2. Provide the parameter values in client-side and pass as an argument for the ajax-before-load event as shown in the following code snippet.

Index.cshtml

<div style="height: 525px;width: 100%;">
    <ej-report-viewer id="reportviewer1" report-service-url="../Home" processing-mode="Local" rendering-begin="renderingBegin" ajax-before-load="ajaxBeforeLoad" />
</div>
<script type="text/javascript">
    function ajaxBeforeLoad(event) {
        var parameters = [{
            name: 'CustomerID',
            labels: ['29889'],
            values: [29889],
            nullable: false
        }];
        event.data = parameters;
    };
</script>
<ej-script-manager></ej-script-manager>

 

3. Get the client-side parameter values in the controller (HomeController) from CustomData key in jsonResult of PostReportAction as shown in the following code snippet.

HomeController.cs

    public partial class HomeController : Controller, IReportController
    {
        public string DefaultParam = null;
        [HttpPost]
        public object PostReportAction([FromBody] Dictionary<string, object> jsonResult)
        {
            if (jsonResult.ContainsKey("CustomData"))
            {
                //Gets the parameter values specified in client-side
                DefaultParam = jsonResult["CustomData"].ToString();
            }
            return ReportHelper.ProcessReport(jsonResult, this, this._cache);
        }
        public void OnInitReportOptions(ReportViewerOptions reportOption)
        {
            string basePath = _hostingEnvironment.WebRootPath;
            FileStream inputStream = new FileStream(basePath + @"\ReportsTemplate\Region.rdlc", FileMode.Open, FileAccess.Read);
            reportOption.ReportModel.Stream = inputStream;
        }
        public void OnReportLoaded(ReportViewerOptions reportOption)
        {
            var parameters = new List<ReportParameter>();
            if (DefaultParam != null)
            {
                parameters = JsonConvert.DeserializeObject<List<ReportParameter>>(DefaultParam);
            }
            if (parameters != null && parameters.Count > 0)
            {
                reportOption.ReportModel.DataSources.Clear();
                //Updates datasource values based on modified report parameter value
                reportOption.ReportModel.DataSources.Add(new ReportDataSource { Name = "StoreSales", Value = StoreSales.GetData(Convert.ToInt32(parameters[0].Values[0])) });
            }
        }
    }

 

Please download the sample for passing report parameter from client to server side here


Conclusion

I hope you enjoyed learning about how to pass a report parameter value from client to server in ASP.NET Core ReportViewer.

You can refer to our ASP.NET Core ReportView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our  ASP.NET Core ReportView example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied