Pass parameter value to MVC report viewer
Environment
Product | Progress® Telerik® Reporting |
Report Viewer | HTML5 ASP.NET MVC |
Description
This article shows how to set the report source and report parameters from the controller in ASP.NET MVC project through a model. The attached project demonstrates the following approach:
Solution
The report contains 2 report parameters- Parameter1 and Parameter2. The initial value of the first one is "Item1" and of the second one is "Value1".
-
Add a new Model called ReportModel. It has 2 properties - a string which holds the ReportName and Dictionary for the parameters:
public class ReportModel { public string ReportName { get; set; } public Dictionary<string,object> Parameters { get; set; } }
Go to the HomeController.
Add the Model namespace through a using.
-
Change the ActionResult method of the page which contains the report viewer as follows:
public ActionResult Index() { Dictionary<string,object>values = new Dictionary<string, object>() { { "Parameter1", "Item2" }, { "Parameter2", "Value2" } }; ReportModel reportModel = new ReportModel() { ReportName = "SampleReport.trdp", Parameters = values }; return View(reportModel); }
-
Go to the page of the report viewer and add the model under the list with usings. For example:
@model PassParameter.Models.ReportModel
-
Change the ReportSource in the initialization of the report viewer as follows:
.ReportSource(Model.ReportName, Model.Parameters)
-
You can also query the report name and parameters in the URL by adding the following Action in the HomeController:
public ActionResult Report(string id, string parameter1, string parameter2) { Dictionary<string, object> values = new Dictionary<string, object>() { { "Parameter1", parameter1 }, { "Parameter2", parameter2 } }; ReportModel reportModel = new ReportModel() { ReportName = $"{id}.trdp", Parameters = values }; return View("Index", reportModel); }
Note that this is in case you use the default MapRoute : url: "{controller}/{action}/{id}". It can be found in App_Start/RouteConfig.cs or in Global.asax file in the MVC project. With another routing may not work this way. Then the URL might look: http://localhost:XXXXX/Home/Report/SampleReport?parameter1=Item3¶meter2=Value3
The demo application could be downloaded from here. Run the Upgrade Wizard before starting the application.