Technology

How to pass parameter to RDLC report in MVC

In this article I am going to explain how to create and pass
parameter to RDLC report in MVC application.

Description:

I want to show report of State population and users can
filter the search for particular state. I am going to use RDLC report to create
and Reportviewer to show report.

Implementation:

Follow the below given steps to create RDLC report in MVC
application:

Step 1:

Before create report you have to add Reportviewer to MVC
application. To add ReportViewer go to Tools >> Library Package Manager >> Package
manger console and paste the following command. After that hit the enter
button.

Install-Package ReportViewerForMvc

After successfully completion of this you will automatically
“ReportViewerWebForm.aspx” page will be added to root of project.

Step 2:

Now add RDLC report to project. How to add RDLC report? I have add
Rptpopulation.rdlc to project and set datasource for it.

Step 3:

Note : I am
using Database first approach.

Structure of Tb_Population.cs
in Model:

namespace MvcApplication4.Models

{

using System;

using System.Collections.Generic;

publicpartialclassTb_Population

{

publicint Id { get; set; }

publicstring StateName { get; set; }

publicInt64 TotalPopulation { get; set; }

}

}

Step 4:

Add an empty controller to project. I have added
ReportsController.cs controller to project and create Actionresult to create
report. Also create Post ActionResult to filter according to statename. Don’t forget to import the Reportviewer
namespace.