【问题标题】:Join 2 tables in report viewer in ASP.NET MVC在 ASP.NET MVC 的报表查看器中加入 2 个表
【发布时间】:2019-09-01 15:28:39
【问题描述】:

我在 ASP.NET MVC 中在 2 个表之间创建了报表查看器。

我创建了适配器来获取报告中的所有数据,如下所示:

但是当我这样导出报告时,expence_type 不显示数据:

这是我的代码:

 public ActionResult Reports(string ReportType)
    {
        LocalReport localreport = new LocalReport();
        localreport.ReportPath = Server.MapPath("~/Reports/ExpenseReport.rdlc");



        ReportDataSource reportDataSource = new ReportDataSource();

        reportDataSource.Name = "DataSetE";

        var dataList = (from ed in _context.ExpenseDetails
         join e in _context.Expenses on ed.ExpensesId equals e.Expenses_Id

     select new { id = ed.ExpenseDetails_Id, amount = ed.Amount, expense = ed.DateExpense, eid = ed.ExpensesId, expenseType = e.Expenses_Type }).ToList();

        reportDataSource.Value = dataList;


        reportDataSource.Value = _context.ExpenseDetails.ToList();

        localreport.DataSources.Add(reportDataSource);

        string reportType = ReportType;
        string mimeType;
        string encoding;
        string fileNameExtension;

        if (reportType == "Excel")
        {
            fileNameExtension = "xlsx";
        }

        if (reportType == "Word")
        {
            fileNameExtension = "docx";
        }

        if (reportType == "PDF")
        {
            fileNameExtension = "pdf";
        }

        else
        {
            fileNameExtension = "jpg";

        }

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

        renderedByte = localreport.Render(reportType, "", out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

        Response.AddHeader("content-disposition", "attachment;filename = expens_report." + fileNameExtension);

        return File(renderedByte, fileNameExtension);
    }

费用明细模型:

public class ExpenseDetails
{
    [Key]
    public int ExpenseDetails_Id { get; set; }

    public double Amount { get; set; }

    public DateTime DateExpense { get; set; }

    public int ExpensesId { get; set; }

    [ForeignKey("ExpensesId")]
    public virtual Expenses expenses { get; set; }
}

费用模型:

   public class Expenses
{
    [Key]
    public int Expenses_Id { get; set; }
    public string Expenses_Type { get; set; }
}

如何显示Expence_Type

reportDataSource.Value = _context.ExpenseDetails.ToList();

我试图在这段代码中添加另一个表,但没有返回。

【问题讨论】:

  • 两张表有关系吗?
  • 是的,它们之间有关系
  • 请帮帮我,我想创建报告
  • 你能把你的表贴在这里或者使用实体框架在两个表之间创建连接并分配它们reportDataSource.Value检查here
  • 我更新了我的 Q 并添加了模型,你可以检查一下吗

标签: c# asp.net-mvc reportviewer


【解决方案1】:

您可以在两个表之间创建连接并将其指定为您的报表数据源

试试这个

var dataList = (from ed in _context.ExpenseDetails
                 join e in _context.Expenses on ed.ExpensesId equals e.Expenses_Id 
                 select new {
                     id = ed.ExpenseDetails_Id ,
                     amount = ed.Amount ,
                     expense = ed.DateExpense ,
                     eid = ed.ExpensesId ,
                     expenseType = e.Expenses_Type 
                 }).ToList();

并添加这个

reportDataSource.Value = dataList ; 

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 2017-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多