C# ASP.NET MVC如何显示基于id的关联数据&x27;s
我试图在我的索引视图中显示基于id相互关联的模型中的数据。即显示客户名称、属于该客户的资产名称以及该客户的地址等 这是我的模型: 客户机型号:C# ASP.NET MVC如何显示基于id的关联数据&x27;s,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我试图在我的索引视图中显示基于id相互关联的模型中的数据。即显示客户名称、属于该客户的资产名称以及该客户的地址等 这是我的模型: 客户机型号: public class Client : Person { public ICollection<OccupancyHistoryRecord> OccupancyRecords { get; set; } public ICollection<RentHistoryRecord> RentRecords
public class Client : Person {
public ICollection<OccupancyHistoryRecord> OccupancyRecords { get; set; }
public ICollection<RentHistoryRecord> RentRecords { get; set; }
}
客户端控制器:
public ActionResult Index()
{
var clients = db.Clients.Include(c => c.OccupancyRecords) // how to get the asset name instead of the id)
.Include(c => c.HomeAddress)
.Include(c => c.WorkAddress);
return View(clients.ToList());
}
索引视图:
@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.OccupancyRecords)
</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.OccupancyRecords)
</td>
<td>
@Html.DisplayFor(modelItem => item.HomeAddress.StreetAddress)
</td>
<td>
@Html.DisplayFor(modelItem => item.WorkAddress.StreetAddress)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }, null) |
@Html.ActionLink("Assets", "Details", "Assets", new { id = item.Id}, null) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数
@ActionLink(“新建”、“创建”)
@DisplayNameFor(model=>model.Name)
@DisplayNameFor(model=>model.occumencyRecords)
@DisplayNameFor(model=>model.HomeAddress)
@DisplayNameFor(model=>model.WorkAddress)
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.Name)
@DisplayFor(modelItem=>item.OccupencyRecords)
@DisplayFor(modelItem=>item.HomeAddress.StreetAddress)
@DisplayFor(modelItem=>item.WorkAddress.StreetAddress)
@ActionLink(“编辑”,“编辑”,新的{id=item.id},空)|
@ActionLink(“资产”、“详细信息”、“资产”,新的{id=item.id},null)|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.id})|
@ActionLink(“删除”,“删除”,新的{id=item.id})
}
现在它正在显示占用记录的Id。我想要的是根据占用的AssetId显示资产名称。
谢谢。您需要更改您的:
public ActionResult Index()
{
var clients = db.Clients.Include(c => c.OccupancyRecords) // how to get the asset name instead of the id)
.Include(c => c.HomeAddress)
.Include(c => c.WorkAddress);
return View(clients.ToList());
}
代码如下:
public ActionResult Index()
{
var clients = db.Clients.Include(c => c.OccupancyRecords.Select(s => new { AssetId = s.AssetId, AssetName = /* Find AssetName By Id here */ }))
.Include(c => c.HomeAddress)
.Include(c => c.WorkAddress);
return View(clients.ToList());
}
public ActionResult Index()
{
var clients = db.Clients.Include(c => c.OccupancyRecords) // how to get the asset name instead of the id)
.Include(c => c.HomeAddress)
.Include(c => c.WorkAddress);
return View(clients.ToList());
}
public ActionResult Index()
{
var clients = db.Clients.Include(c => c.OccupancyRecords.Select(s => new { AssetId = s.AssetId, AssetName = /* Find AssetName By Id here */ }))
.Include(c => c.HomeAddress)
.Include(c => c.WorkAddress);
return View(clients.ToList());
}