我正在使用 ASP.NET 6 MVC 應用程式并使用此方法檢索控制器中的“添加”視圖。
// GET: Operations/Bookings/Add
public IActionResult Add()
{
ViewData["ClientList"] = _context.Clients.ToList<Client>();
ViewData["CountryList"] = _context.Countries.ToList<Country>();
return View();
}
我們用于ViewData["ClientList"]
檢索客戶的完整串列,以供用戶選擇將新的“預訂”記錄與哪一個關聯:
<fieldset>
<div class="card mb-0">
<div class="card-body table-responsive p-0">
<table class="table table-sm table table-striped table-hover text-nowrap">
<thead>
<tr>
<th>Name</th>
<th style="width: 250px">City</th>
<th style="width: 250px">Country</th>
</tr>
</thead>
<tbody>
@foreach (var Item in ViewBag.ClientList)
{
<tr>
<td>
<div class="icheck-primary d-inline">
<input asp-for="ClientId" id="@Item.Id" type="radio" name="ClientItem" value="@Item.Id" />
<label for="@Item.Id" class="w-100" style="font-weight: 400">@Item.CompanyName</label>
</div>
</td>
<td>
<label for="@Item.Id" class="w-100 mb-0" style="font-weight: 400">
@Item.City
</label>
</td>
<td>
<label for="@Item.Id" class="w-100 mb-0" style="font-weight: 400">
<span class="flag-icon [email protected]() table-icon"></span>
@Item.Country.CountryName
</label>
</td>
</tr>
-
}
</tbody>
</table>
</div>
</div>
</fieldset>
以下是預訂、客戶和國家/地區模型:
public class Bookings
{
public int Id { get; set; }
public int Type { get; set; }
[Display(Name = "Client")]
public int ClientId { get; set; }
public virtual Client? Client { get; set; }
//Truncated
// . . .
}
public class Client
{
public int Id { get; set; }
[Display(Name = "Company Name")]
public string CompanyName { get; set; }
// Address
[Display(Name = "Address")]
public string Address1 { get; set; }
[Display(Name = " ")]
public string? Address2 { get; set; }
public string City { get; set; }
public string County { get; set; }
[Display(Name = "Country")]
public int CountryID { get; set; }
public virtual Country? Country { get; set; }
[Display(Name = "Postal Code")]
public string PostalCode { get; set; }
//Truncated
// . . .
}
public class Country
{
public int Id { get; set; }
public string CountryName { get; set; }
public string CountryCode { get; set; }
}
這里讓我感到困惑的是,簡單地添加ViewData["CountryList"]
到控制器代碼會導致客戶端的Country
物件/屬性填充正確的資料。如果我洗掉這行代碼,則此 Country 物件為 null 并且請求頁面會導致錯誤,因為我正在從 null 物件請求資訊。這是我期待的。
我插入了一個斷點return View();
以便能夠檢查這些值。請注意CountryId
它的 int 值如何存盤在資料庫中,但如果沒有該ViewData["CountryList"]
行,則 Country 物件為空。為清楚起見,我的資料集中的國家 ID 234 是英國,與第一個螢屏截圖中顯示的國家相匹配。
和ViewData["CountryList"]
沒有ViewData["CountryList"]
如何填充正確的國家資料?我看不到我在 View 中的參考方式或使用此 ViewData 成員填充 Client 中ViewData["CountryList"]
的屬性的任何其他方式。Country
MVC 中是否有一些內部作業可以為我處理這個問題?它是不應該起作用的東西,但以某種方式起作用嗎?
uj5u.com熱心網友回復:
您應該閱讀 Entity Framework 的檔案。特別是在查詢相關資料時,它說:
Entity Framework Core將自動修復先前加載到背景關系實體中的任何其他物體的導航屬性。因此,即使您沒有明確包含導航屬性的資料,如果之前加載了部分或所有相關物體,該屬性仍可能會被填充。
如果您想對要加載的內容進行更多隱式控制,可以使用Include
. 就像是:
ViewData["ClientList"] = _context.Clients
.Include(client => client.Country)
.ToList<Client>();
應該做的伎倆。
uj5u.com熱心網友回復:
MVC 中是否有一些內部作業可以為我處理這個問題?
@Wiktor Zychla 的評論是對的,您可以閱讀 Read related data - ASP.NET MVC with EF Core了解更多資訊。
首次讀取物體時,不會檢索相關資料。但是,第一次嘗試訪問導航屬性時,會自動檢索該導航屬性所需的資料。每次您第一次嘗試從導航屬性獲取資料時,都會向資料庫發送一個查詢。
如何填充正確的國家資料?
如果你使用 With ViewData["CountryList"]
,你可以得到結果。如果你使用 without ViewData["CountryList"]
,避免 Country 物件為空,你可以使用Include
. 這是獲得結果的兩種方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/517084.html
標籤:C#asp.net-mvc实体框架asp.net 核心实体框架核心
上一篇:無法定位主鍵,因為它不兼容
下一篇:保存物體在資料庫中重復導航屬性