I have a form which is used to generate a Report. We are using RDLC
reports and the Report is loaded in an aspx
page.
So this is the code for the Form
, form target is set to _blank
, and opens in new Tab.
@using (Html.BeginForm("AssetReports", "AssetReports", FormMethod.Post, new { target = "_blank" }))
{
<div class="row mt-15">
<div class="col-md-12 text-center">
<input type="submit" class="btn btn-primary" value="Show Report" />
</div>
</div>
}
This is the Controller action which redirects to the Report aspx page, where the Report is processed and displayed.
[HttpPost]
public void AssetReports(AssetReportsDTO model, AssetReportParametersDTO reportParameters)
{
SessionHandler.AssetReport = model;
SessionHandler.AssetReportParameters = reportParameters;
switch (model.SelectedReportType)
{
case AssetReportTypesEnum.ExcessiveIdleReport:
Response.Redirect("~/Reports/AssetReports/ExcessiveIdleReport/ExcessiveIdleReport.aspx");
break;
}
}
The Reports take 3,4 minutes to generate in some cases. and during this time the UI is blocked,
We want the report to generate on a separate thread so that user can use the UI while the report is generated.
Is there a way in MVC C# to Execute this Action in a separate Thread?
I have tried using the following, but the Context and Session are then NULL
Task.Factory.StartNew(() =>
{
switch (model.SelectedReportType)
{
case AssetReportTypesEnum.ExcessiveIdleReport:
Response.Redirect("~/Reports/AssetReports/ExcessiveIdleReport/ExcessiveIdleReport.aspx");
break;
}
});
and also:
new Thread(() =>
{
switch (model.SelectedReportType)
{
case AssetReportTypesEnum.ExcessiveIdleReport:
Response.Redirect("~/Reports/AssetReports/ExcessiveIdleReport/ExcessiveIdleReport.aspx");
break;
}
}).Start();
EDIT
Code to generate the Report in - This is the code that takes 3 to 4 minutes ExcessiveIdleReport.aspx
public partial class ExcessiveIdleReport1 : Page
{
private IReportsProvider _reportsProvider;
protected void Page_Load(object sender, EventArgs e)
{
_reportsProvider = new ReportsProvider();
if (!IsPostBack)
{
try
{
var reportDetails = SessionHandler.AssetReport;
var reportParams = SessionHandler.AssetReportParameters;
var sPath = Server.MapPath("../ExcessiveIdleReport/ExcessiveIdleReport.rdlc");
var dsExcessiveReport =
_reportsProvider.GetExcessiveIdleReport(reportDetails.CompanyId, reportDetails.AssetId, reportDetails.StartDate,
reportDetails.EndDate, reportParams.SelectedIdleTime * 60);
ExcessiveIdleReportViewer.ProcessingMode = ProcessingMode.Local;
ExcessiveIdleReportViewer.LocalReport.EnableHyperlinks = true;
ExcessiveIdleReportViewer.HyperlinkTarget = "_blank";
ExcessiveIdleReportViewer.LocalReport.DataSources.Add(new ReportDataSource("ExcessiveIdleReport", dsExcessiveReport.Tables[0]));
ExcessiveIdleReportViewer.LocalReport.DataSources.Add(new ReportDataSource("ReportHeaderDetails", dsExcessiveReport.Tables[1]));
ExcessiveIdleReportViewer.LocalReport.DataSources.Add(new ReportDataSource("ReportSummary", dsExcessiveReport.Tables[2]));
ExcessiveIdleReportViewer.LocalReport.ReportPath = sPath;
ExcessiveIdleReportViewer.LocalReport.EnableExternalImages = true;
ExcessiveIdleReportViewer.LocalReport.SetParameters(param);
ExcessiveIdleReportViewer.LocalReport.Refresh();
}
catch (Exception ex)
{
ErrorDiv.InnerText = string.Format("An error occured while generating the ExcessiveIdleReport, Please contact Support with following Message: [{0}] - [{1}]", ex.Message, ex.StackTrace);
ReportContentDiv.Visible = false;
ErrorDiv.Visible = true;
}
}
}
}
I have also tried using Ajax.BeginForm
@using (Ajax.BeginForm("AssetReports", "AssetReports", new AjaxOptions() { HttpMethod = "POST", OnSuccess = "OpenReport"}, new { target = "_blank" }))
{
<div class="row mt-15">
<div class="col-md-12 text-center">
<input type="submit" class="btn btn-primary" value="Show Report" />
</div>
</div>
}
JS:
function OpenReport(response) {
var popup = window.open("about:blank", "_blank"); // the about:blank is to please Chrome, and _blank to please Firefox
popup.location = '/TBReports/AssetReports/ExcessiveIdleReport/ExcessiveIdleReport.aspx';
}
I load all other Pages using Ajax:
Here is an image of the Asset Reports page 'Show Report' button which executes the action:
But once this button is clicked other UI Elements are Blocked. e.g. I can't load View with Group Reports
until the Report has been generated.