将报告绑定到web mvc2中的reportviewer

cpo*_*ign 2 reportviewer visual-studio-2008 asp.net-mvc-2

我有asp.net MVC2应用程序.我正在使用VS2008,并希望将生成的报告从我的控制器连接到reportviewer.

有任何想法吗?

到目前为止我有这个代码"控制器"

//should pass data to report
 public ActionResult GenerateReport()
      {


        LocalReport report = new LocalReport();
        report.ReportPath = Server.MapPath("~/Reports/KingsCourt.rdlc");

        List<InvoiceRow> rows = new List<InvoiceRow>();

        rows.Add(new InvoiceRow { name = "Testing item", value = (decimal)25.85 });
        rows.Add(new InvoiceRow { name = "Testing item2", value = (decimal)5.15 });
        ReportDataSource source = new ReportDataSource("InvoiceRow", rows);
        report.DataSources.Add(source);

        ViewData["InvoiceRow"] = report;
        return View();
      }
Run Code Online (Sandbox Code Playgroud)

和查看页面:

 <form id="form1" runat="server">

  <h2>GenerateReport</h2>
  <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" 
      Font-Size="8pt" Height="400px" Width="400px">
    <LocalReport ReportPath="Reports\KingsCourt.rdlc">
      <DataSources>
        <rsweb:ReportDataSource DataSourceId="ObjectDataSource1" Name="InvoiceRow" />
      </DataSources>
    </LocalReport>


  </rsweb:ReportViewer>    

  <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Rows" 
      TypeName="Accounts.Classes.Invoices"></asp:ObjectDataSource>
 </form>
Run Code Online (Sandbox Code Playgroud)

Lef*_*tyX 9

您始终可以将ASP.NET WebForms与MVC一起使用.我想这是唯一的方法.
我在这里为你准备了一个样品.

您可以创建一个文件夹,放置您的asp.net WebForm和报告(rdlc).我把架构(xsd)和数据(xml)放在同一文件夹中,但很明显,我猜你要使用数据库.我已经将路由映射到这样的报告:

    //Custom route for reports
    routes.MapRoute(
     "ReportRoute",
     "Reports/{reportname}",                
     "~/Reports/{reportname}.aspx"
     );
Run Code Online (Sandbox Code Playgroud)

更新:

我已经为ASP.NET MVC3(MvcReportViewerMVC3)准备了一些代码.
它与webform上的一些小改动几乎相同:我已经集成了新的ReportViewer(10),我不得不将ScriptManager添加到同一页面:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
Run Code Online (Sandbox Code Playgroud)

我已经更改了WebForm代码,因为看起来page_load事件被称为load次:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
    ....
    }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.

  • MVC3在这里......当使用这种方法时,我得到"controllerName不能为空"(路由在默认路由之前注册) (2认同)