我有一個 .NET 5 專案Index頁面,其中包含表中所有行的表Contracts,每行都有一Contract_StatusID列,從表中保存 FK Contract_Statuses- 表Contracts有更多具有此邏輯的列
事實是,顯式加載僅完全加載表的最后一行,其他行的列是隨機的null,即使集合.Include(s => s.Contract_Status)來自控制器代碼。設定了所有ID列,這些ID存在于相關表中,但未在Index頁面加載到該表中。
Detail頁面始終正確加載所有.Include()關系表,但不在Index頁面上。
我已經驗證了我的模型,它們看起來不錯,并且可以在網站的其他頁面中使用。
我缺少什么?已經為此瀏覽了 MSDN 和 EFCore 教程,但它不起作用。

public async Task<IActionResult> Index()
{
var contract = await context.Contracts
.Include(c => c.Employee)
.ThenInclude(c => c.Gender)
.Include(i => i.Business_Unit)
.Include(i => i.Brand)
.Include(i => i.Contract_Status)
.Include(i => i.Statutory_Unit)
.Include(i => i.Management_Market).ToListAsync();
return View(contract);
}
編輯:添加我的 Index.cshtml
@model IEnumerable<MyProject.Models.Contract>
<table >
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Employee)
</th>
<th>
@Html.DisplayNameFor(model => model.Contract_Status.Contract_Status)
</th>
<th>
@Html.DisplayNameFor(model => model.Business_Unit.Business_Unit)
</th>
<th>
@Html.DisplayNameFor(model => model.Brand.Brand)
</th>
<th>
@Html.DisplayNameFor(model => model.Statutory_Unit.Statutory_Unit)
</th>
<th>
@Html.DisplayNameFor(model => model.Management_Market.Management_Market)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Employee.EmployeeID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Contract_Status.Contract_Status)
</td>
<td>
@Html.DisplayFor(modelItem => item.Business_Unit.Business_Unit)
</td>
<td>
@Html.DisplayFor(modelItem => item.Brand.Brand)
</td>
<td>
@Html.DisplayFor(modelItem => item.Statutory_Unit.Statutory_Unit)
</td>
<td>
@Html.DisplayFor(modelItem => item.Management_Market.Management_Market)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.ContractID">Edit</a> |
<a asp-action="Details" asp-route-id="@item.ContractID">Details</a>
</td>
</tr>
}
</tbody>
</table>
我的Contract模型與相關表的示例
public class Contract
{
[Key]
[Required]
public Guid ContractID { get; set; }
[Required]
public string EmployeeID { get; set; }
public Employee Employee { get; set; }
[Required]
public int Contract_StatusID { get; set; }
public CONF_Contract_Status Contract_Status { get; set; }
[Required]
public int Business_UnitID { get; set; }
public CONF_Business_Unit Business_Unit { get; set; }
[Required]
public int BrandID { get; set; }
public CONF_Brand Brand { get; set; }
[Required]
public int Statutory_UnitID { get; set; }
public CONF_Statutory_Unit Statutory_Unit { get; set; }
[Required]
public int Management_MarketID { get; set; }
public CONF_Management_Market Management_Market { get; set; }
}
public class CONF_Contract_Status
{
[Key]
public int CS_ID { get; set; }
[Required]
[Display(Name = "Status")]
public string Contract_Status { get; set; }
[Required]
public bool Enabled { get; set; }
public Contract Contract { get; set; }
}
Contract表的
資料庫截圖
和 CONF_Contract_Status

uj5u.com熱心網友回復:
我通常總是為索引視圖創建視圖模型,因為它通常需要幾個字串列,但它可以有很多列。如果您只需要一列,則無需從服務器獲取整個記錄
public class ContractVieModel
{
public Guid ContractID { get; set; }
public string EmployeeName { get; set; }
public string Contract_Status { get; set; }
public string Business_Unit { get; set; }
.... and so on
}
動作,你不要顯式地使用 Include
public async Task<IActionResult> Index()
{
var model = await context.Contracts
.Select(i=> new ContractViewModel
{
Id=i.Id,
EmployeeName=i.Employee.Name
Contract_Status =i.Contract_Status.Contract_Status,
Business_Unit = i.Business_Unit.Business_Unit,
... and so on
}).ToListAsync();
return View(model);
}
看法
@model IEnumerable<ContractViewModel>
......
試試看,如果它仍然不起作用,那么你將不得不使用左外連接來獲得一個視圖模型
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/322261.html
