【问题标题】:How to add report parameter in asp.net mvc如何在asp.net mvc中添加报表参数
【发布时间】:2014-01-29 08:13:52
【问题描述】:

我在报告中添加了 2 个参数,称为 dtStartDate 和 dtEndDate。我试图弄清楚如何在控制器上实现。目前它在尝试设置参数时会抛出错误。请指教,谢谢

以下是我到目前为止的代码:

public ActionResult DetailsReport()
{

    LocalReport localReport = new LocalReport();
    localReport.ReportPath = Server.MapPath("~/Content/Reports/Data.rdlc");

    ReportParameter param0 = new ReportParameter("dtStartDate", "2014-01-28");
    ReportParameter param1 = new ReportParameter("dtStartEnd", "2014-01-30");

    localReport.SetParameters(new ReportParameter[] { param0, param1 });
    ReportDataSource reportDataSource = new ReportDataSource("dsData", GetAllData());

    localReport.DataSources.Add(reportDataSource);
    string reportType = "PDF";
    string mimeType;
    string encoding;
    string fileNameExtension;

    //The DeviceInfo settings should be changed based on the reportType
    string deviceInfo =
    "<DeviceInfo>" +
    "  <OutputFormat>PDF</OutputFormat>" +
    "  <PageWidth>8.5in</PageWidth>" +
    "  <PageHeight>11in</PageHeight>" +
    "  <MarginTop>0.5in</MarginTop>" +
    "  <MarginLeft>1in</MarginLeft>" +
    "  <MarginRight>1in</MarginRight>" +
    "  <MarginBottom>0.5in</MarginBottom>" +
    "</DeviceInfo>";

    Warning[] warnings;
    string[] streams;
    byte[] renderedBytes;

    //Render the report
    renderedBytes = localReport.Render(
        reportType,
        deviceInfo,
        out mimeType,
        out encoding,
        out fileNameExtension,
        out streams,
        out warnings);
    Response.AddHeader("content-disposition", "attachment; filename=Data." + fileNameExtension);
    return File(renderedBytes, mimeType);


}

public static List<vwDataReport> GetAllData()
{
    var entities = new DataEntities();
    var x = from c in entities.vwDataReport
            select c;
    return x.ToList();
}

【问题讨论】:

  • 什么是异常,你从哪里得到它?

标签: c# asp.net asp.net-mvc asp.net-mvc-3 razor


【解决方案1】:

使用 i.e. 中的两个日期创建一个 ViewModel

public class ReportModel
{
   public DateTime StartDate { get; set; }
   public DateTime StartEnd { get; set; }
}

在您的 Get 方法中初始化并发送回它,这将呈现一个日期选择屏幕,即

public ActionResult DetailsReport()
{
     var model = new ReportModel();
     model.StartDate = DateTime.Now;
     model.StartEnd = DateTime.Now;
     return View(model);
}

在您的视图中渲染控件以显示它们,即,如果您使用类似这样的 jQuery UI:

@model ReportModel

@using(Html.BeginForm()){
@Html.TextBoxFor(m => m.StartDate, String.Format("{0:dd/M/yy}", Model.StartDate),
  new { @class = "datepicker datepicker-init" })

@Html.TextBoxFor(m => m.StartEnd, String.Format("{0:dd/M/yy}", Model.StartEnd),
  new { @class = "datepicker datepicker-init" })
<input type="submit" value="submit" />
}

<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/jquery-ui-1.10.3.js"></script>
<script type="text/javascript">
        $(".datepicker").datepicker({
            minDate: 0,
            dateFormat: 'dd/mm/y'
        });

</script>

使用以下签名创建一个帖子操作,该操作将使用选定的日期将模型传回,然后充实您的报告处理代码,其中 ... 即:

[HttpPost]
public ActionResult DetailsReport(ReportModel model)
{
    ...
    var startDate = model.StartDate.ToString();
    var startEnd = model.StartEnd.ToString();
    ReportParameter param0 = new ReportParameter("dtStartDate", startDate);
    ReportParameter param1 = new ReportParameter("dtStartEnd", startEnd);
    ...
}

【讨论】:

    猜你喜欢
    • 2016-02-26
    • 2011-07-03
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    相关资源
    最近更新 更多