【发布时间】: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