The data binding support, allows you to view RDLC reports that exist on the local file system with JSON array and custom business object data collection. The following steps demonstrates how to render a RDLC report with JSON array and 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.
Set the RDLC report path to the ReportPath property.
Assign the ProcessingMode property to ProcessingMode.Local.
Bind the data from viewdata collection to the DataSources property as shown in 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;
}
}The following steps help you to configure the Web API to render the RDLC report with 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 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
Nameis case sensitive and it should be same as in the data source name in the report definition. TheValueaccepts IList, DataSet, and DataTable inputs.
To load report as a stream, 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 above code,
product-list.rdlcreport is loaded from theResourcesfolder location.