Render RDLC report
To render an RDLC report in the Bold Reports® ASP.NET MVC Report Viewer, load the report from the local file system and bind a JSON array or a custom business object data collection as its data source. The following steps demonstrate how to render an RDLC report with a JSON array and a custom business object data collection.
Add the RDLC report
product-list.rdlcfrom Bold Reports® installation location to your applicationResourcesfolder. For more information, see Samples and demos.
How do I bind a data source on the client side?
To bind a data source to the ASP.NET MVC Report Viewer on the client side, follow these steps.
-
Set the RDLC report path to the
ReportPathproperty. -
Assign the
ProcessingModeproperty toProcessingMode.Local. -
Bind the data from
viewdatacollection to theDataSourcesproperty as in the following code:@(Html.Bold().ReportViewer("viewer") .ProcessingMode(BoldReports.ReportViewerEnums.ProcessingMode.Local) .ReportPath("~/Resources/product-list.rdlc") .ReportServiceUrl("/api/ReportViewer") .DataSources(ds => ds.Name("list").Value(ViewData["DataSource"]).Add()) ) -
Create a class and methods that returns business object data collection. Use the following code in your application Web API Service.
public partial class HomeController : Controller { public ActionResult Index() { ProductList productList = new ProductList(); ViewData["DataSource"] = productList.GetData(); return View(); } } public class ProductList { public string ProductName { get; set; } public string OrderId { get; set; } public double Price { get; set; } public string Category { get; set; } public string Ingredients { get; set; } public string ProductImage { get; set; } public IList GetData() { List<ProductList> dataList = new List<ProductList>(); ProductList data = null; data = new ProductList() {ProductName = "Baked Chicken and Cheese",OrderId = "323B60",Price = 55,Category = "Non-Veg",Ingredients = "grilled chicken, corn and olives.",ProductImage = "" }; dataList.Add(data); data = new ProductList() {ProductName = "Chicken Delite",OrderId = "323B61",Price = 100,Category = "Non-Veg",Ingredients = "cheese, chicken chunks, onions & pineapple chunks.",ProductImage = ""}; dataList.Add(data); data = new ProductList() {ProductName = "Chicken Tikka",OrderId = "323B62",Price = 64,Category = "Non-Veg",Ingredients = "onions, grilled chicken, chicken salami & tomatoes.",ProductImage = ""}; dataList.Add(data); return dataList; } }
Bind data source in Web API controller
The following steps help you configure the Report Viewer Web API controller to render the RDLC report with a business object data collection.
-
Create a class and methods that returns business object data collection. Use the following code in your application Web API Service.
public class ProductList { public string ProductName { get; set; } public string OrderId { get; set; } public double Price { get; set; } public string Category { get; set; } public string Ingredients { get; set; } public string ProductImage { get; set; } public static IList GetData() { List<ProductList> dataList = new List<ProductList>(); ProductList data = null; data = new ProductList() {ProductName = "Baked Chicken and Cheese",OrderId = "323B60",Price = 55,Category = "Non-Veg",Ingredients = "grilled chicken, corn and olives.",ProductImage = "" }; dataList.Add(data); data = new ProductList() {ProductName = "Chicken Delite",OrderId = "323B61",Price = 100,Category = "Non-Veg",Ingredients = "cheese, chicken chunks, onions & pineapple chunks.",ProductImage = ""}; dataList.Add(data); data = new ProductList() {ProductName = "Chicken Tikka",OrderId = "323B62",Price = 64,Category = "Non-Veg",Ingredients = "onions, grilled chicken, chicken salami & tomatoes.",ProductImage = ""}; dataList.Add(data); return dataList; } } -
Set the
ProcessingModetoProcessingMode.LocalandReportPathto the RDLC report location. -
Bind the business object data values collection by adding a new item to the
DataSourcesas in the following code snippet:[NonAction] public void OnInitReportOptions(ReportViewerOptions reportOption) { reportOption.ReportModel.ProcessingMode = ProcessingMode.Local; reportOption.ReportModel.ReportPath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/Resources/product-list.rdlc"); reportOption.ReportModel.DataSources.Add(new BoldReports.Web.ReportDataSource { Name = "list", Value = ProductList.GetData() }); }Here the
Nameis case sensitive and it should be same as in the data source name in the report definition. TheValueaccepts IList, DataSet, and DataTable inputs.
How do I load a report as a stream?
To load a report as a stream in the ASP.NET MVC Report Viewer, create a report stream using the FileStream class and assign the report stream to the Stream property.
[NonAction]
public void OnInitReportOptions(ReportViewerOptions reportOption)
{
// Opens the report from application Resources folder using FileStream
FileStream reportStream = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath(@"~/Resources/product-list.rdlc"), FileMode.Open, FileAccess.Read);
reportOption.ReportModel.Stream = reportStream;
reportOption.ReportModel.ProcessingMode = ProcessingMode.Local;
reportOption.ReportModel.DataSources.Add(new BoldReports.Web.ReportDataSource { Name = "list", Value = ProductList.GetData() });
}In the previous code, the
product-list.rdlcreport is loaded from theResourcesfolder location.