国クラスを含むモデル顧客がいます。国モデルと顧客モデルの両方がデータベースにマップされます。
[Table("Customer")]
public class Customer
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CustomerID { get; set; }
public string Prefix { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
public string Address { get; set; }
public string CountryID { get; set; }
[ForeignKey("CountryID")]
public Country Country { get; set; }
public string City { get; set; }
[Display(Name = "Postcode")]
public string PostCode { get; set; }
public string Email { get; set; }
public string Remarks { get; set; }
[Display(Name = "Telephone")]
public string PhoneNumber { get; set; }
}
顧客のリストを生成しようとするとき。国名を生成することはありません。データベースを調べたときにすべてのフィールドが入力されていても。
@foreach (var item in Model)
{
<tr>
<td align="center"><input name="select_row[]" type="checkbox" /></td>
<td>@Html.DisplayFor(modelItem => item.Prefix)</td>
<td>@Html.DisplayFor(modelItem => item.FirstName)</td>
<td>@Html.DisplayFor(modelItem => item.LastName)</td>
<td>@Html.DisplayFor(modelItem => item.Country.Name)</td>
<td>@Html.DisplayFor(modelItem => item.City)</td>
<td>@Html.DisplayFor(modelItem => item.Address)</td>
<td>@Html.DisplayFor(modelItem => item.PhoneNumber)</td>
<td>@Html.DisplayFor(modelItem => item.Email)</td>
<td>@Html.DisplayFor(modelItem => item.Remarks)</td>
<td><span onclick="function_panel('customer','edit','@Url.Action("Edit", "Customer", new { id = item.CustomerID })')">edit</span>
|
<span onclick="function_panel('customer','delete','@Url.Action("Delete","Customer")')">del</span></td>
</tr>
}