我剛開始使用 SQL 并試圖讓我的第一個示例作業。我有兩個具有一對多關系的表:
public class Customer
{
public Guid Id { get; set; }
public string CompanyName { get; set; }
public string Address { get; set; }
[JsonIgnore]
public virtual ICollection<Project> Projects { get; set; }
}
public class Project
{
public Guid Id { get; set; }
public string Name { get; set; }
public string ProjectType { get; set; }
public Guid CustomerId { get; set; }
public Customer Customer { get; set; }
}
然后我通過 API 執行以下命令ProjectsController:
[HttpGet]
public async Task<IActionResult> Get()
{
System.Collections.Generic.List<Project> projects =
await _context.Projects.Include(d => d.Customer).ToListAsync();
return Ok(projects);
}
我得到我的
private IEnumerable<Project> ProjectsList { get; set; } = new List<Project>();
在剃刀頁面上,與
this.ProjectsList = await this.HttpClient.GetFromJsonAsync<IEnumerable<Project>>("api/Projects");
我已經檢查ProjectsList了除錯器的資料結構,看起來不錯。
但是:Razor 頁表(兩列“專案名稱”和“客戶名稱”):
<Table DataSource="ProjectsList" TItem="Project">
<Column TData="string"
Title="Name"
@bind-Field="context.Name"
SorterCompare="@((a,b)=> string.Compare(a,b))"
SortDirections="new[] { SortDirection.Descending }"
Filterable />
<Column TData="string"
@bind-Field="context.Customer.CompanyName"
SorterCompare="@((a,b)=> string.Compare(a,b))"
SortDirections="new[] { SortDirection.Descending }"
Filterable />
</Table>
我得到一個物件未??設定為物件實體的例外。我猜這是因為context.Customer.CompanyNamewhereCustomer沒有初始化?
首先,我有一個問題,我應該重做我的Project模型并在那里添加額外的字串型別列,CompanyName還是有其他方法可以在剃刀頁面上顯示資料?
如果……我應該在Project.cs如何使用 linq 查詢資料中添加額外的列?
uj5u.com熱心網友回復:
首先,請嘗試以下操作,如果出現錯誤,請在此處發布其文本:
public Customer Customer { get; set; } = new Customer {};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/373539.html
