Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#MVC3中使用reportviewer_C#_Asp.net Mvc 3_Reportviewer_Ascx - Fatal编程技术网

如何在C#MVC3中使用reportviewer

如何在C#MVC3中使用reportviewer,c#,asp.net-mvc-3,reportviewer,ascx,C#,Asp.net Mvc 3,Reportviewer,Ascx,我有一个生成报告的函数。它起作用了。我的问题是,我在C#中使用MVC3,无法在文件中插入reportviewer。Cshtml。我正在使用Ascx尝试显示报告,但出现以下错误: 执行处理程序'System.Web.Mvc.HttpHandlerUtil ServerExecuteHttpHandlerRapper+'的子请求时出错。 当我打电话时o@Html.Partialrelatorio.cshtml文件中的(“relatorioApontamento”) relatorio.cshtml

我有一个生成报告的函数。它起作用了。我的问题是,我在C#中使用MVC3,无法在文件中插入reportviewer。Cshtml。我正在使用Ascx尝试显示报告,但出现以下错误:

执行处理程序'System.Web.Mvc.HttpHandlerUtil ServerExecuteHttpHandlerRapper+'的子请求时出错。

当我打电话时o@Html.Partialrelatorio.cshtml文件中的(“relatorioApontamento”)

relatorio.cshtml

@{
    ViewBag.Title = "relatorio";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script src="@Url.Content("~/Scripts/relatorio.js")" type="text/javascript"></script>
@using (Html.BeginForm("relatorio", "Paginas", FormMethod.Post, new { @id = "frmParametroConfigPath" }))
{
   <div id="relatorio">
        <h1 class="titulo">Relatório</h1>
        <div class="boxRecurso">
            <label id="lbl_recurso">Recurso:</label><br />
            <select ID="ddl_nm_recurso" class="txtRecurso"></select>
        </div>
        <div class="boxDataInicial">
            <label id="lbl_data_inicial">Data Inicial:</label><br />
            <input type="text" id="datepicker_ida" />
        </div>
        <div class="boxDataFinal">
            <label id="lbl_data_final">Data Final:</label><br />
            <input type="text" id="datepicker_volta" />
        </div>
        <div id="box_btnGerar">
            <input type="button" ID="btnGerar" class="botao" value="Gerar" />
        </div>
    </div>
    @Html.Partial("relatorioApontamento");
在Paginas控制器中的功能

public int gerarRelatorioRelatorio(DateTime datepicker_ida, DateTime datepicker_volta, string ddl_nm_recurso)
        {
            try
            {
                ProjectBoxDAL dalProjectBox = new ProjectBoxDAL();
                Softbox.DashBordGBU.Reports.dtsReportRecurso dt = new Softbox.DashBordGBU.Reports.dtsReportRecurso();

                BUProjetosDAL dalBuProjetos = new BUProjetosDAL();

                int codRecurso = Convert.ToInt32(ddl_nm_recurso);
                int codCliente = dalBuProjetos.retornaCodigoClienteRecurso(codRecurso);

                IDataReader dr = dalProjectBox.relatorioRecurso(codCliente, datepicker_ida, datepicker_volta, codRecurso);

                dt.Tables[0].Load(dr);

                if (dt.Tables[0].Rows.Count > 0)
                {
                    var total = dt.ReportRecurso.AsEnumerable().Sum(x => x.horas_ms);

                    TimeSpan totalHoras = TimeSpan.FromMilliseconds(total);

                    Microsoft.Reporting.WebForms.ReportDataSource item = new Microsoft.Reporting.WebForms.ReportDataSource();
                    item.Value = dt.Tables[0];
                    item.Name = "ReportRecurso";

                    ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
                    Session["DataInicio"] = datepicker_ida;
                    Session["DataFim"] = datepicker_volta;
                    Session["ddl"] = ddl_nm_recurso;
                    Session["TotalHoras"] = totalHoras;
                    Session["Item"] = item;

                    return 1;
                }
                else
                    return 2;

            }
            catch (Exception)
            {
                return 0;
            }
        }

您不需要新视图或局部视图来显示报表:)只需执行以下操作: 在Paginas控制器的末尾添加新的方法调用print() 在“打印”方法中,定义报告并将其打印为pdf格式,如下所示:

     public void Print()
                {
        LocalReport localReport = new LocalReport();
        localReport.ReportPath = @"report full path [Reports/myreport.rdlc]";
//if you have parameters set your parameters here
           Warning[] warnings;
                    string[] streamids;
                    string mimeType;
                    string encoding;
                    string filenameExtension;

                    byte[] rebytes = localReport.Render(
        "PDF", null, out mimeType, out encoding, out filenameExtension,
        out streamids, out warnings);

                    Response.Buffer = true;
                    Response.Clear();
                    Response.ContentType = mimeType;
                    Response.AddHeader("application/pdf", "attachment; filename= filename" + "." + filenameExtension);
                    Response.OutputStream.Write(rebytes, 0, rebytes.Length); // create the file  

                    Response.Flush(); // send it to the client to download  
                    Response.End();
        }

看起来您将ASP.NET MVC和Web窗体混合在一起。MVC中没有“用户控件”。我在其他文件中使用MVC和Web表单,它可以工作。我的Paginas控制器中有这样一个功能。不要使用报表查看器,以实用的方式打印报表,就像我发布的代码一样。我只需要在我的网页中显示报表,我不需要生成pdfSorry,现在我明白了。我尝试过,但没有在这里工作,因为它在一些参数中变为null。如何继续?var参数=新报告参数[1];//报表参数[0]中有多少个参数=新报表参数(“Parametername”,ParameterValue;localReport.SetParameters(参数);
public int gerarRelatorioRelatorio(DateTime datepicker_ida, DateTime datepicker_volta, string ddl_nm_recurso)
        {
            try
            {
                ProjectBoxDAL dalProjectBox = new ProjectBoxDAL();
                Softbox.DashBordGBU.Reports.dtsReportRecurso dt = new Softbox.DashBordGBU.Reports.dtsReportRecurso();

                BUProjetosDAL dalBuProjetos = new BUProjetosDAL();

                int codRecurso = Convert.ToInt32(ddl_nm_recurso);
                int codCliente = dalBuProjetos.retornaCodigoClienteRecurso(codRecurso);

                IDataReader dr = dalProjectBox.relatorioRecurso(codCliente, datepicker_ida, datepicker_volta, codRecurso);

                dt.Tables[0].Load(dr);

                if (dt.Tables[0].Rows.Count > 0)
                {
                    var total = dt.ReportRecurso.AsEnumerable().Sum(x => x.horas_ms);

                    TimeSpan totalHoras = TimeSpan.FromMilliseconds(total);

                    Microsoft.Reporting.WebForms.ReportDataSource item = new Microsoft.Reporting.WebForms.ReportDataSource();
                    item.Value = dt.Tables[0];
                    item.Name = "ReportRecurso";

                    ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
                    Session["DataInicio"] = datepicker_ida;
                    Session["DataFim"] = datepicker_volta;
                    Session["ddl"] = ddl_nm_recurso;
                    Session["TotalHoras"] = totalHoras;
                    Session["Item"] = item;

                    return 1;
                }
                else
                    return 2;

            }
            catch (Exception)
            {
                return 0;
            }
        }
     public void Print()
                {
        LocalReport localReport = new LocalReport();
        localReport.ReportPath = @"report full path [Reports/myreport.rdlc]";
//if you have parameters set your parameters here
           Warning[] warnings;
                    string[] streamids;
                    string mimeType;
                    string encoding;
                    string filenameExtension;

                    byte[] rebytes = localReport.Render(
        "PDF", null, out mimeType, out encoding, out filenameExtension,
        out streamids, out warnings);

                    Response.Buffer = true;
                    Response.Clear();
                    Response.ContentType = mimeType;
                    Response.AddHeader("application/pdf", "attachment; filename= filename" + "." + filenameExtension);
                    Response.OutputStream.Write(rebytes, 0, rebytes.Length); // create the file  

                    Response.Flush(); // send it to the client to download  
                    Response.End();
        }