diff --git a/AlanJuden.MvcReportViewer.ExampleWebsite.NetCore/Views/Report/ReportViewer.cshtml b/AlanJuden.MvcReportViewer.ExampleWebsite.NetCore/Views/Report/ReportViewer.cshtml index bf0f0a0..63a7f42 100644 --- a/AlanJuden.MvcReportViewer.ExampleWebsite.NetCore/Views/Report/ReportViewer.cshtml +++ b/AlanJuden.MvcReportViewer.ExampleWebsite.NetCore/Views/Report/ReportViewer.cshtml @@ -22,7 +22,7 @@ "); + return new HtmlString(sb.ToString()); } + + public static string ParametersToHtmlString(System.Collections.Generic.List parameters, ReportViewerModel model) + { + StringBuilder sb = new StringBuilder(); + + if (parameters == null) + { + var contentData = new ReportExportResult(); + var definedParameters = ReportServiceHelpers.GetReportParameters(model, true); + contentData.SetParameters(definedParameters, model.Parameters); + parameters = contentData.Parameters; + } + + //Parameters start + foreach (var reportParameter in parameters) + { + sb.AppendLine("
"); + if (reportParameter.PromptUser || model.ShowHiddenParameters) + { + sb.AppendLine($"
"); + + sb.AppendLine("
"); + if (reportParameter.ValidValues != null && reportParameter.ValidValues.Any()) + { + sb.AppendLine($" "); + } + else + { + var selectedValue = reportParameter.SelectedValues.FirstOrDefault(); + + if (reportParameter.Type == ReportService.ParameterTypeEnum.Boolean) + { + sb.AppendLine($" "); + } + else if (reportParameter.Type == ReportService.ParameterTypeEnum.DateTime) + { + sb.AppendLine($" "); + } + else + { + sb.AppendLine($" "); + } + } + + sb.AppendLine("
"); + } + else + { + if (reportParameter.SelectedValues != null && reportParameter.SelectedValues.Any()) + { + var values = reportParameter.SelectedValues.Where(x => x != null).Select(x => x).ToArray(); + sb.AppendLine($" "); + } + } + + sb.AppendLine($" "); + + sb.AppendLine("
"); + } + + return sb.ToString(); + } } } diff --git a/AlanJuden.MvcReportViewer.NetCore/ReportController.cs b/AlanJuden.MvcReportViewer.NetCore/ReportController.cs index c7ac3e7..7fda298 100644 --- a/AlanJuden.MvcReportViewer.NetCore/ReportController.cs +++ b/AlanJuden.MvcReportViewer.NetCore/ReportController.cs @@ -145,6 +145,15 @@ public JsonResult FindStringInReport(string reportPath, string searchText, int? return Json(ReportServiceHelpers.FindStringInReport(model, searchText, page).ToInt32()); } + public JsonResult ReloadParameters(string reportPath) + { + var model = this.GetReportViewerModel(Request); + model.ViewMode = ReportViewModes.View; + model.ReportPath = reportPath; + + return Json(AlanJuden.MvcReportViewer.CoreHtmlHelpers.ParametersToHtmlString(null, model)); + } + public ActionResult PrintReport(string reportPath) { var model = this.GetReportViewerModel(Request); diff --git a/AlanJuden.MvcReportViewer.NetCore/ReportExportResult.cs b/AlanJuden.MvcReportViewer.NetCore/ReportExportResult.cs index 1e3e1fb..82d92bf 100644 --- a/AlanJuden.MvcReportViewer.NetCore/ReportExportResult.cs +++ b/AlanJuden.MvcReportViewer.NetCore/ReportExportResult.cs @@ -34,6 +34,7 @@ internal void SetParameters(ReportService.ReportParameter[] definedReportParamet { var reportParameter = new ReportParameterInfo(); reportParameter.AllowBlank = definedReportParameter.AllowBlank; + reportParameter.Dependencies = definedReportParameter.Dependencies; reportParameter.MultiValue = definedReportParameter.MultiValue; reportParameter.Name = definedReportParameter.Name; reportParameter.Prompt = definedReportParameter.Prompt; diff --git a/AlanJuden.MvcReportViewer.NetCore/ReportParameterInfo.cs b/AlanJuden.MvcReportViewer.NetCore/ReportParameterInfo.cs index 0bdd9d9..7dd30e4 100644 --- a/AlanJuden.MvcReportViewer.NetCore/ReportParameterInfo.cs +++ b/AlanJuden.MvcReportViewer.NetCore/ReportParameterInfo.cs @@ -15,7 +15,8 @@ public class ReportParameterInfo public List SelectedValues { get; set; } public ReportService.ParameterTypeEnum Type { get; set; } public bool PromptUser { get; set; } - public bool AllowBlank { get; internal set; } + public bool AllowBlank { get; set; } + public string[] Dependencies { get; set; } public ReportParameterInfo() { diff --git a/AlanJuden.MvcReportViewer.NetCore/ReportServiceHelpers.cs b/AlanJuden.MvcReportViewer.NetCore/ReportServiceHelpers.cs index e06e528..01848f1 100644 --- a/AlanJuden.MvcReportViewer.NetCore/ReportServiceHelpers.cs +++ b/AlanJuden.MvcReportViewer.NetCore/ReportServiceHelpers.cs @@ -75,6 +75,25 @@ public static ReportService.ReportParameter[] GetReportParameters(ReportViewerMo ReportService.ParameterValue[] values = null; ReportService.DataSourceCredentials[] rsCredentials = null; + if (model != null && model.Parameters != null && model.Parameters.Any()) + { + var tempParameters = new List(); + foreach (var parameter in model.Parameters) + { + foreach (var value in parameter.Value.Where(x => !String.IsNullOrEmpty(x))) + { + tempParameters.Add(new ReportService.ParameterValue() + { + Label = parameter.Key, + Name = parameter.Key, + Value = value + }); + } + } + + values = tempParameters.ToArray(); + } + var parameters = service.GetReportParametersAsync(model.ReportPath, historyID, forRendering, values, rsCredentials).Result; return parameters; diff --git a/AlanJuden.MvcReportViewer.NetCore/ReportViewerModel.cs b/AlanJuden.MvcReportViewer.NetCore/ReportViewerModel.cs index 53f2d2c..e795f14 100644 --- a/AlanJuden.MvcReportViewer.NetCore/ReportViewerModel.cs +++ b/AlanJuden.MvcReportViewer.NetCore/ReportViewerModel.cs @@ -81,13 +81,14 @@ public void AddParameter(string name, string[] values) } } + private static List KEYS_TO_IGNORE = new List() { "ReportViewerEnablePaging", "reportPath", "startPage", "searchText", "page" }; private static string[] VALUE_SEPARATORS = new string[] { "," }; public void BuildParameters(HttpRequest request) { if (request.Query != null && request.Query.Keys != null) { - foreach (var key in request.Query.Keys) + foreach (var key in request.Query.Keys.Where(x => !KEYS_TO_IGNORE.Contains(x))) { this.AddParameter(key, request.Query[key].ToSafeString().ToStringList(VALUE_SEPARATORS).ToArray()); } @@ -97,7 +98,7 @@ public void BuildParameters(HttpRequest request) { if (request.Form != null && request.Form.Keys != null) { - foreach (var key in request.Form?.Keys) + foreach (var key in request.Form?.Keys.Where(x => !KEYS_TO_IGNORE.Contains(x))) { this.AddParameter(key, request.Form[key].ToSafeString().ToStringList(VALUE_SEPARATORS).ToArray()); } diff --git a/AlanJuden.MvcReportViewer/HtmlHelperExtensions.cs b/AlanJuden.MvcReportViewer/HtmlHelperExtensions.cs index 0fc4e2b..03b5891 100644 --- a/AlanJuden.MvcReportViewer/HtmlHelperExtensions.cs +++ b/AlanJuden.MvcReportViewer/HtmlHelperExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; @@ -19,57 +20,8 @@ public static MvcHtmlString RenderReportViewer(this HtmlHelper helper, ReportVie sb.AppendLine("
"); sb.AppendLine("
"); sb.AppendLine("
"); - //Parameters start - foreach (var reportParameter in contentData.Parameters) - { - sb.AppendLine("
"); - if (reportParameter.PromptUser || model.ShowHiddenParameters) - { - sb.AppendLine($"
"); - - sb.AppendLine("
"); - if (reportParameter.ValidValues != null && reportParameter.ValidValues.Any()) - { - sb.AppendLine($" "); - } - else - { - var selectedValue = reportParameter.SelectedValues.FirstOrDefault(); - - if (reportParameter.Type == ReportService.ParameterTypeEnum.Boolean) - { - sb.AppendLine($" "); - } - else if (reportParameter.Type == ReportService.ParameterTypeEnum.DateTime) - { - sb.AppendLine($" "); - } - else - { - sb.AppendLine($" "); - } - } - - sb.AppendLine("
"); - } - else - { - if (reportParameter.SelectedValues != null && reportParameter.SelectedValues.Any()) - { - var values = reportParameter.SelectedValues.Where(x => x != null).Select(x => x).ToArray(); - sb.AppendLine($" "); - } - } - sb.AppendLine($" "); - - sb.AppendLine("
"); - } + sb.AppendLine(ParametersToHtmlString(contentData.Parameters, model)); sb.AppendLine("
"); @@ -166,7 +118,100 @@ public static MvcHtmlString RenderReportViewer(this HtmlHelper helper, ReportVie sb.AppendLine("
"); sb.AppendLine(""); + sb.AppendLine(""); + return new MvcHtmlString(sb.ToString()); } + + public static string ParametersToHtmlString(System.Collections.Generic.List parameters, ReportViewerModel model) + { + StringBuilder sb = new StringBuilder(); + + if (parameters == null) + { + var contentData = new ReportExportResult(); + var definedParameters = ReportServiceHelpers.GetReportParameters(model, true); + contentData.SetParameters(definedParameters, model.Parameters); + parameters = contentData.Parameters; + } + + //Parameters start + foreach (var reportParameter in parameters) + { + sb.AppendLine("
"); + if (reportParameter.PromptUser || model.ShowHiddenParameters) + { + sb.AppendLine($"
"); + + sb.AppendLine("
"); + if (reportParameter.ValidValues != null && reportParameter.ValidValues.Any()) + { + sb.AppendLine($" "); + } + else + { + var selectedValue = reportParameter.SelectedValues.FirstOrDefault(); + + if (reportParameter.Type == ReportService.ParameterTypeEnum.Boolean) + { + sb.AppendLine($" "); + } + else if (reportParameter.Type == ReportService.ParameterTypeEnum.DateTime) + { + sb.AppendLine($" "); + } + else + { + sb.AppendLine($" "); + } + } + + sb.AppendLine("
"); + } + else + { + if (reportParameter.SelectedValues != null && reportParameter.SelectedValues.Any()) + { + var values = reportParameter.SelectedValues.Where(x => x != null).Select(x => x).ToArray(); + sb.AppendLine($" "); + } + } + + sb.AppendLine($" "); + + sb.AppendLine("
"); + } + + return sb.ToString(); + } } } diff --git a/AlanJuden.MvcReportViewer/ReportController.cs b/AlanJuden.MvcReportViewer/ReportController.cs index b003bd2..d6cfd3a 100644 --- a/AlanJuden.MvcReportViewer/ReportController.cs +++ b/AlanJuden.MvcReportViewer/ReportController.cs @@ -129,6 +129,15 @@ public JsonResult FindStringInReport(string reportPath, string searchText, int? return Json(ReportServiceHelpers.FindStringInReport(model, searchText, page).ToInt32(), JsonRequestBehavior.AllowGet); } + public JsonResult ReloadParameters(string reportPath) + { + var model = this.GetReportViewerModel(Request); + model.ViewMode = ReportViewModes.View; + model.ReportPath = reportPath; + + return Json(AlanJuden.MvcReportViewer.HtmlHelperExtensions.ParametersToHtmlString(null, model), JsonRequestBehavior.AllowGet); + } + public ActionResult PrintReport(string reportPath) { var model = this.GetReportViewerModel(Request); diff --git a/AlanJuden.MvcReportViewer/ReportExportResult.cs b/AlanJuden.MvcReportViewer/ReportExportResult.cs index e767b8d..5ebd9f0 100644 --- a/AlanJuden.MvcReportViewer/ReportExportResult.cs +++ b/AlanJuden.MvcReportViewer/ReportExportResult.cs @@ -31,6 +31,7 @@ internal void SetParameters(ReportService.ReportParameter[] definedReportParamet { var reportParameter = new ReportParameterInfo(); reportParameter.AllowBlank = definedReportParameter.AllowBlank; + reportParameter.Dependencies = definedReportParameter.Dependencies; reportParameter.MultiValue = definedReportParameter.MultiValue; reportParameter.Name = definedReportParameter.Name; reportParameter.Prompt = definedReportParameter.Prompt; diff --git a/AlanJuden.MvcReportViewer/ReportParameterInfo.cs b/AlanJuden.MvcReportViewer/ReportParameterInfo.cs index c11afec..a7bf137 100644 --- a/AlanJuden.MvcReportViewer/ReportParameterInfo.cs +++ b/AlanJuden.MvcReportViewer/ReportParameterInfo.cs @@ -11,7 +11,8 @@ public class ReportParameterInfo public List SelectedValues { get; set; } public ReportService.ParameterTypeEnum Type { get; set; } public bool PromptUser { get; set; } - public bool AllowBlank { get; internal set; } + public bool AllowBlank { get; set; } + public string[] Dependencies { get; set; } public ReportParameterInfo() { diff --git a/AlanJuden.MvcReportViewer/ReportServiceHelpers.cs b/AlanJuden.MvcReportViewer/ReportServiceHelpers.cs index 4d05d15..b3c05eb 100644 --- a/AlanJuden.MvcReportViewer/ReportServiceHelpers.cs +++ b/AlanJuden.MvcReportViewer/ReportServiceHelpers.cs @@ -40,6 +40,25 @@ public static ReportService.ReportParameter[] GetReportParameters(ReportViewerMo ReportService.ParameterValue[] values = null; ReportService.DataSourceCredentials[] rsCredentials = null; + if (model != null && model.Parameters != null && model.Parameters.Any()) + { + var tempParameters = new List(); + foreach (var parameter in model.Parameters) + { + foreach (var value in parameter.Value.Where(x => !String.IsNullOrEmpty(x))) + { + tempParameters.Add(new ReportService.ParameterValue() + { + Label = parameter.Key, + Name = parameter.Key, + Value = value + }); + } + } + + values = tempParameters.ToArray(); + } + var parameters = service.GetReportParameters(model.ReportPath, historyID, forRendering, values, rsCredentials); return parameters; diff --git a/AlanJuden.MvcReportViewer/ReportViewerModel.cs b/AlanJuden.MvcReportViewer/ReportViewerModel.cs index f8b32fe..cb9223e 100644 --- a/AlanJuden.MvcReportViewer/ReportViewerModel.cs +++ b/AlanJuden.MvcReportViewer/ReportViewerModel.cs @@ -79,16 +79,17 @@ public void AddParameter(string name, string[] values) } } + private static List KEYS_TO_IGNORE = new List() { "ReportViewerEnablePaging", "reportPath", "startPage", "searchText", "page" }; private static string[] VALUE_SEPARATORS = new string[] { "," }; public void BuildParameters(HttpRequestBase request) { - foreach (var key in request.QueryString.AllKeys) + foreach (var key in request.QueryString.AllKeys.Where(x => !KEYS_TO_IGNORE.Contains(x))) { this.AddParameter(key, request.QueryString[key].ToSafeString().ToStringList(VALUE_SEPARATORS).ToArray()); } - foreach (var key in request.Form.AllKeys) + foreach (var key in request.Form.AllKeys.Where(x => !KEYS_TO_IGNORE.Contains(x))) { this.AddParameter(key, request.Form[key].ToSafeString().ToStringList(VALUE_SEPARATORS).ToArray()); }