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.rdlc from Bold Reports® installation location to your application Resources folder. 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.

  1. Set the RDLC report path to the ReportPath property.

  2. Assign the ProcessingMode property to ProcessingMode.Local.

  3. Bind the data from viewdata collection to the DataSources property 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())
    )
  4. 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 ProcessingMode to ProcessingMode.Local and ReportPath to the RDLC report location.

  • Bind the business object data values collection by adding a new item to the DataSources as 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 Name is case sensitive and it should be same as in the data source name in the report definition. The Value accepts 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.rdlc report is loaded from the Resources folder location.