I am fairly new to ASP.Net MVC. I have a requirement of showing an RDLC based report in MVC.
Basically my requirement as well as what I have done is :-
I have a ReportController inheriting APIController, which has a method that returns a DataSet. This DataSet is being sent to the RDLC file.
For this, I have done the following, but could not make the report work.
I have created a model class named ReportParameter as follows:
public class ReportParameter
{
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
}
I have the following controller ReportViewController :
public class ReportViewController : Controller
{
static readonly ReportController ctrl = new ReportController();
public ActionResult GenerateReport()
{
return View();
}
[HttpPost]
public ActionResult GenerateReport(ReportParameterSalesOrder param)
{
if (ModelState.IsValid)
{
Helpers.DataLayer dl = new Helpers.DataLayer();
if (param.DateFrom != null)
{
DateTime DateFrom = Convert.ToDateTime(param.DateFrom);
DateTime DateTo = Convert.ToDateTime(param.DateTo);
string fdate = DateFrom.ToString("yyyy/MM/dd");
string tdate = DateTo.ToString("yyyy/MM/dd");
Session["ReportSales"] = ctrl.ReportSales(param);
}
return Redirect(Url.Action("ViewReport", "ReportView"));
}
return View();
}
public ActionResult ViewReport()
{
return View();
}
}
I have an API Controller ReportController whose object has been created in the above ReportViewerController to generate a DataSet and to populate the RDLC report. The API Controller is:
public class ReportController : ApiController
{
static readonly IReportRepository repository = new ReportRepository();
[ActionName("ReportSales")]
public DataSet ReportSales(ReportParameterSalesOrder paramSO)
{
DataSet item = repository.ReportSales(paramSO);
if (item == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return item;
}
}
I have two views GenerateReport.aspx and ViewReport.aspx. The GenerateReport.aspx is given below:
<table style="width: 40%;">
<tr>
<td class="style1">
<h3>
<asp:Label ID="Label1" runat="server" Text="From Date"></asp:Label></h3>
</td>
<td>
<%=@Html.EditorFor(a=> a.DateFrom, new{id="startDate",style="width:250px;"}) %>
<%=@Html.ValidationMessageFor(a => a.DateFrom)%>
</td>
</tr>
<tr>
<td class="style1">
<h3>
<asp:Label ID="Label2" runat="server" Text="To Date"></asp:Label></h3>
</td>
<td>
<%=@Html.EditorFor(a => a.DateTo, new { id = "ToDate", style = "width: 250px;" })%>
<%=@Html.ValidationMessageFor(a => a.DateTo)%>
</td>
</tr>
<tr>
<td class="style1">
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
</td>
<td>
<input id="btnsearch" class="button" type="submit" value="Show" />
</td>
</tr>
</table>
The ViewReport.aspx is given below:
<center style="width: 974px">
<iframe id="myReport" width="100%" height="450px" src="ReportViewer.aspx">
</iframe></center>
I have added a Dataset.xsd, an rdlc file and an aspx page to add the rdlc file.
But I cannot make it work. How do I display the report, or how do I populate the dataset that I receive from the Controller to the report ?