尝试访问模型的嵌入类型的 属性
Trying to access the property of an embedded type of a model
我无法显示嵌入类型的属性。
如何在 razor 页面中访问嵌入类型模型(例如 CustomerOrderItem.Meal.Name)的 属性?
剃刀页面:
@page
@model RazorPages.Pages.BasketOrder.IndexModel
@{
}
@if (Model.ShowMessage)
{
<div asp-validation-summary="All" class="alert alert-info">@Model.Message</div>
}
<form method="post">
<table class="table">
<thead>
<tr>
<th>@Html.DisplayNameFor(m => m.CustomerOrderItems[0].Meal.Name)</th>
</tr>
</thead>
<tbody>
@foreach (var m in Model.CustomerOrderItems)
{
<tr>
<td>@m.Meal.Name</td>
</tr>
}
</tbody>
</table>
</form>
在 'Code behind' 文件中,OnGetAsync - 方法获取 CustomerOrderItems。
public class IndexModel : PageModel
{
private ApplicationDbContext _db;
private UserManager<ApplicationUser> _userManager;
public IList<CustomerOrderItem> CustomerOrderItems { get; set; }
[TempData]
public string Message { get; set; }
public bool ShowMessage => !string.IsNullOrEmpty(Message);
public IndexModel(ApplicationDbContext db, UserManager<ApplicationUser> userManager)
{
_db = db;
_userManager = userManager;
}
public async Task OnGetAsync()
{
CustomerOrderItems = await _db.CustomerOrderItems.ToListAsync();
}
}
模型 类、CustomOrderItem 引用了 Meal。
public class CustomerOrderItem
{
public int Id { get; set; }
public int Status { get; set; }
public CustomerOrder CustomerOrder { get; set; }
public Meal Meal { get; set; }
public ICollection<Extra> Extras { get; set; }
}
public class Meal
{
public int Id { get; set; }
public int Status { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
用餐是顾客内心的具体客体。 EF core 2 没有延迟加载,但这不是一个大问题。
您可以使用 eagar 加载:
//With eagerloading DbSet
//has method called include. Dont abuse eagerloading slows down db
CustomerOrderItems = await _db.CustomerOrderItems
.Include(x => x.Meals).ToListAsync();
//also initialize that Collection in your constructor if you want to use it
我无法显示嵌入类型的属性。 如何在 razor 页面中访问嵌入类型模型(例如 CustomerOrderItem.Meal.Name)的 属性?
剃刀页面:
@page
@model RazorPages.Pages.BasketOrder.IndexModel
@{
}
@if (Model.ShowMessage)
{
<div asp-validation-summary="All" class="alert alert-info">@Model.Message</div>
}
<form method="post">
<table class="table">
<thead>
<tr>
<th>@Html.DisplayNameFor(m => m.CustomerOrderItems[0].Meal.Name)</th>
</tr>
</thead>
<tbody>
@foreach (var m in Model.CustomerOrderItems)
{
<tr>
<td>@m.Meal.Name</td>
</tr>
}
</tbody>
</table>
</form>
在 'Code behind' 文件中,OnGetAsync - 方法获取 CustomerOrderItems。
public class IndexModel : PageModel
{
private ApplicationDbContext _db;
private UserManager<ApplicationUser> _userManager;
public IList<CustomerOrderItem> CustomerOrderItems { get; set; }
[TempData]
public string Message { get; set; }
public bool ShowMessage => !string.IsNullOrEmpty(Message);
public IndexModel(ApplicationDbContext db, UserManager<ApplicationUser> userManager)
{
_db = db;
_userManager = userManager;
}
public async Task OnGetAsync()
{
CustomerOrderItems = await _db.CustomerOrderItems.ToListAsync();
}
}
模型 类、CustomOrderItem 引用了 Meal。
public class CustomerOrderItem
{
public int Id { get; set; }
public int Status { get; set; }
public CustomerOrder CustomerOrder { get; set; }
public Meal Meal { get; set; }
public ICollection<Extra> Extras { get; set; }
}
public class Meal
{
public int Id { get; set; }
public int Status { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
用餐是顾客内心的具体客体。 EF core 2 没有延迟加载,但这不是一个大问题。
您可以使用 eagar 加载:
//With eagerloading DbSet
//has method called include. Dont abuse eagerloading slows down db
CustomerOrderItems = await _db.CustomerOrderItems
.Include(x => x.Meals).ToListAsync();
//also initialize that Collection in your constructor if you want to use it