【问题标题】:ASP.NET MVC How to display aggregation dataASP.NET MVC 如何显示聚合数据
【发布时间】:2017-04-06 02:00:49
【问题描述】:

我不太清楚这个术语是什么,因为我对 ASP.NET MVC EF 还很陌生。我正在尝试将使用我的 create actionresult 函数创建的用户显示到我的索引视图上,例如姓名、工作地址、家庭地址、资产名称等......

到目前为止,我只能通过这样做来正确显示姓名和家庭地址:

 public ActionResult Index()
 {
      var clients = db.Clients.Include(c => c.HomeAddress);
      return View(clients.ToList());
 }

我将如何显示其他数据以及资产名称、工作地址等?

我的客户模型:

public class Client : Person {
    public ICollection<OccupancyHistoryRecord> OccupancyRecords { get; set;     }

        public ICollection<RentHistoryRecord> RentRecords { get; set; }

        public Asset Assets { get; set; }
    }
}

人物模型:

public class Person {
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Display(Name = "Home Address")]
    public FullAddress HomeAddress { get; set; }

    [Display(Name = "Work Address")]
    public FullAddress WorkAddress { get; set; }
}

全地址模型:

public Class FullAddress {
    public int Id { get; set; }

    [Display(Name = "Suite")]
    public string UnitNum { get; set; }

    [Display(Name = "Street Address")]
    public string StreetAddress { get; set; }

    public string Province { get; set; }

    public string Country { get; set; }

    [Display(Name = "Postal Code")]
    public string PostalCode { get; set; }
}

资产模型:

public Class Asset {
    public int Id { get; set; }

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

    [Display(Name = "Asset Type")]
    public string Type { get; set; }

    public FullAddress Address { get; set; }

    [Display(Name = "Asking Rent")]
    public string AskingRent { get; set; }

    public ICollection<OccupancyHistoryRecord> OccupancyRecords;

    public ICollection<RentHistoryRecord> RentRecords;

}

到目前为止我的索引视图:

@model IEnumerable<RentalManagement.Models.Client>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
    <table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Assets.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Assets.Type)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.HomeAddress) 
        </th>
        <th>
            @Html.DisplayNameFor(model => model.WorkAddress)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Assets.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Assets.Type)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.HomeAddress.StreetAddress)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.WorkAddress)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

感谢任何帮助。谢谢!

【问题讨论】:

    标签: c# entity-framework asp.net-mvc-4


    【解决方案1】:

    包括所有虚拟属性:

    EF Core 版本:

    public ActionResult Index()
    {
        var clients = db.Clients
            .Include(p => p.Assets)
                .ThenInclude(p => p.Address)
            .Include(p => p.Assets)
                .ThenInclude(p => p.OccupancyRecords)
            .Include(p => p.Assets)
                .ThenInclude(p => p.RentRecords)
            .Include(p => p.HomeAddress)
            .Include(p => p.WorkAddress)
            .ToList();
        return View(clients);
    }
    

    EF 6 版本:

    public ActionResult Index()
    {
        var clients = db.Clients
            .Include(p => p.Assets.Select(s => s.Address))
            .Include(p => p.Assets.Select(s => s.OccupancyRecords))
            .Include(p => p.Assets.Select(s => p.RentRecords))
            .Include(p => p.HomeAddress)
            .Include(p => p.WorkAddress)
            .ToList();
        return View(clients);
    }
    

    还将此属性标记为虚拟以启用延迟加载机制:

    public virtual ICollection<OccupancyHistoryRecord> OccupancyRecords;
    public virtual ICollection<RentHistoryRecord> RentRecords;
    

    【讨论】:

    • 似乎没有适合我的 ThenInclude 函数?
    • .ThenInclude 是一个 EF Core 扩展。我编辑了我的答案并发布了 EF 6 版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多