C# 尝试访问模型的嵌入类型的属性

C# 尝试访问模型的嵌入类型的属性,c#,asp.net,razor-pages,C#,Asp.net,Razor Pages,我无法显示嵌入类型的属性。 如何在razor页面中访问模型的嵌入类型(例如,CustomerOrderItem.Dine.Name)的属性 剃须刀页面: @page @model RazorPages.Pages.BasketOrder.IndexModel @{ } @if (Model.ShowMessage) { <div asp-validation-summary="All" class="alert alert-info">@Model.Message</

我无法显示嵌入类型的属性。 如何在razor页面中访问模型的嵌入类型(例如,CustomerOrderItem.Dine.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>
@page
@模型RazorPages.Pages.BasketOrder.IndexModel
@{
}
@if(Model.ShowMessage)
{
@模型消息
}
@DisplayNameFor(m=>m.CustomerOrderItems[0].Dine.Name)
@foreach(Model.CustomerOrderItems中的var m)
{
@m、 餐名
}
在“代码隐藏”文件中,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();
    }
}
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; }
}
公共类索引模型:PageModel
{
私有应用程序上下文数据库;
私人用户管理器(UserManager);;
公共IList CustomerOrderItems{get;set;}
[临时数据]
公共字符串消息{get;set;}
public bool ShowMessage=>!string.IsNullOrEmpty(消息);
公共索引模型(ApplicationDbContext数据库,UserManager UserManager)
{
_db=db;
_userManager=userManager;
}
公共异步任务OnGetAsync()
{
CustomerOrderItems=等待_db.CustomerOrderItems.ToListSync();
}
}
模型类,CustomOrderItem引用了膳食

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();
    }
}
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; }
}
公共类CustomerOrderItem
{
公共int Id{get;set;}
公共int状态{get;set;}
公共CustomerOrder CustomerOrder{get;set;}
公共餐{get;set;}
公共ICollection Extras{get;set;}
}
公共课餐
{
公共int Id{get;set;}
公共int状态{get;set;}
公共字符串名称{get;set;}
公共双价{get;set;}
}

餐点是客户内部的一个具体创建对象。EFcore 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

您使用的是什么版本的EF?我使用EF Core 2。是的,您可以使用eagar加载、显式加载和投影查询。