【问题标题】:Asp.Net Mvc, Rdlc reports - How do I show associated name from parent table instead of just it's ID?Asp.Net Mvc,Rdlc 报告-如何显示父表中的关联名称而不仅仅是它的 ID?
【发布时间】:2015-10-03 12:40:03
【问题描述】:

我在一个大学项目中使用 Asp.net MVC 5EF 6。我正在使用 RDLC 来生成报告。

我创建了一个显示项目及其相关详细信息的简单报告。但我不知道如何显示项目的制造商名称而不是其制造商 ID。

谁能帮我解决这个问题?

已经这么多天了,我还在为报告而苦恼。看起来,使用 webforms 和 ADO.net 很容易。但是我发现很难使用实体框架将报表合并到 MVC 中。

如果有更好的选择,请告诉我。

对于这份报告,我参考了 this tutorial

这是我生成报告的代码:

public ActionResult Report(string id)
    {
        LocalReport lr = new LocalReport();
        string path = Path.Combine(Server.MapPath("~/Reports"), "Report_item.rdlc");
        if (System.IO.File.Exists(path))
        {
            lr.ReportPath = path;
        }
        else
        {
            return View("Index");
        }
        List<Item> cm = new List<Item>();            
        cm = db.Items.ToList();

        ReportDataSource rd = new ReportDataSource("MyData_Item", cm);
        lr.DataSources.Add(rd);
        string reportType = id;
        string mimeType;
        string encoding;
        string fileNameExtension;

        string deviceInfo =
        "<DeviceInfo>" +
        "  <OutputFormat>" + id + "</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;

        renderedBytes = lr.Render(
            reportType,
            deviceInfo,
            out mimeType,
            out encoding,
            out fileNameExtension,
            out streams,
            out warnings);


        return File(renderedBytes, mimeType);
    }

物品型号:

public class Item
{
    public int ID { get; set; }

    [Required]
    [Display(Name="Item")]
    public string Name { get; set; }

    [Display(Name="Generic Name")]
    public int? DrugGenericNameID { get; set; }

    [Display(Name = "Manufacturer")]
    public int? ManufacturerID { get; set; }

    [Display(Name = "Categeory")]
    public Categeory? Categeory { get; set; }   

    public int AlertQty { get; set; }
    public string Description { get; set; }

    [Display(Name = "Last Update")]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime? LastUpdated { get; set; }


    //reference entity
    public virtual DrugGenericName DrugGenericName { get; set; }
    public virtual Manufacturer Manufacturer { get; set; }
    public virtual ICollection<Stock> Stocks { get; set; }
    public virtual ICollection<PurchaseItem> PurchaseItems { get; set; }

}
public enum Categeory
{
    Drug,
    Supplies,
    other
}

这是我的报告。如何派生关联名称而不是其 ID?

【问题讨论】:

  • 你能用表格/字段发布你的数据源的表示吗?
  • 为了更清晰,我添加了模型和报告图像。希望对您有所帮助。

标签: asp.net-mvc-4 visual-studio-2013 entity-framework-6 rdlc


【解决方案1】:

通常,RDLC 报告对其数据模型使用相当扁平的结构 - 您可以在报告中拥有关联的子表;但是对于报表中的平面表,所有字段都应该在同一个表/模型中。

因此,将其视为您的项目模型(为简洁起见,删除了大部分原始属性):

public class Item
{
    public int ID { get; set; }

    [Required]
    [Display(Name="Item")]
    public string Name { get; set; }

    // Properties as per your original model ...


    //reference entity
    public virtual DrugGenericName DrugGenericName { get; set; }
    public virtual Manufacturer Manufacturer { get; set; }
    public virtual ICollection<Stock> Stocks { get; set; }
    public virtual ICollection<PurchaseItem> PurchaseItems { get; set; }

    // Added properties:
    public string ManufacturerName {
       get { return Manufacturer.Name; }
    }

    // etc.
}

然后您就可以直接引用制造商名称。

【讨论】:

  • 但是有了这个,我必须更新数据库,这个过程会产生冗余。不是吗?
  • 在这种情况下,只需使用代表报表布局的 DTO 对象并将您的属性映射到它上面(您可以使用 Automapper 来减少工作量)做同样的事情
  • 无需更新数据库 - 您应该能够在模型中创建被数据库忽略的“只读”列(您可能必须使用适合您的实体框架的属性来装饰它们)。否则,只需按照 Charleh 的建议创建一个 DTO 对象,然后使用 Automapper 映射两者即可。
猜你喜欢
  • 1970-01-01
  • 2020-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多