子表模型在部分视图中始终为空 [英] Model for child table is always null in the partial view
问题描述
在我的 ASP.NET Core 1.1
, EF-Core 1.1应用程序
中, 具有
。情景类似于以下内容:部分视图
的父视图 Customers和Orders表
之间的一对多关系。用户从父视图中的下拉列表中选择一个客户,该客户具有显示所选客户相应订单的局部视图。但是,我总是将传递给部分视图的模型始终为空。
问题:我可能在这里缺少什么?
$ b 注意:通过放置断点,我已验证下面的控制器正确地将所选客户的记录发送到父视图,而且我也在Db中检查,该客户在订单表中有相应的订单记录。
家长:
public class Customer
{
[Key]
public int CustomerId {get;组; }
public string Name {get;组; }
....
....
public Order order {get;组; }
}
小孩:
public class Order b $ b {
[Key]
public int OrderId {get;组; }
public float Price {get;组; }
....
....
public int? CustomerId {get;组; }
public客户{get;组; }
$ <$> $ <$> :
....
....
Customer mycustomer = _context.Customer.Where(c => c.CustomerId == selectedId).SingleOrDefault();
....
....
return View(mycustomer);
父视图:
@model MyProj.Models.Customer
....
@ Html.Partial(Partial_orders,Model.order)
...
部分视图:
@model MyProj.Models.Order
....
< div> @Model。客户ID< DIV>
....
解决方案加载订单,不能显示。
EntityFramework Core不支持延迟加载。所以,当你打电话给
Customer mycustomer = _context.Customer
.Where(c => c.CustomerId == selectedId).SingleOrDefault();
只有客户会加载。您必须用 .Include
或使用 .Load
Customer mycustomer = _context.Customer
.Include(c => c.order)//添加此行以告知EF Core也加载订单
。其中(c => c.CustomerId == selectedId).SingleOrDefault();
或者:
Customer mycustomer = _context.Customer
.Where(c => c.CustomerId == selectedId).SingleOrDefault();
//显式加载异步
await _context.Entry(mycustomer)
.Reference(c => c.order)
.LoadAsync();
//显式加载同步
_context.Entry(mycustomer)
.Reference(c => c.order)
.Load();
In my ASP.NET Core 1.1
, EF-Core 1.1 app
, I'm displaying data in a parent view
that has partial view
. Scenario is similar to the following: one-to-many relationship between Customers and Orders table
. User selects a customer from a dropdown in the Parent View that has a partial view that displays corresponding orders for the selected customer. But, I'm always getting model passed to the Partial view always null.
Question: What I may be missing here?
Note: By placing breakpoints, I've verified that the controller below is correctly sending the selected customer's record to the Parent View, and I've also checked in Db that that customer has corresponding order records in orders table.
Parent:
public class Customer
{
[Key]
public int CustomerId{ get; set; }
public string Name { get; set; }
....
....
public Order order { get; set; }
}
Child:
public class Order
{
[Key]
public int OrderId{ get; set; }
public float Price { get; set; }
....
....
public int? CustomerId { get; set; }
public Customer customer{ get; set; }
}
Controller:
....
....
Customer mycustomer = _context.Customer.Where(c=> c.CustomerId== selectedId).SingleOrDefault();
....
....
return View(mycustomer);
Parent View:
@model MyProj.Models.Customer
....
@Html.Partial("Partial_orders", Model.order)
....
Partial View:
@model MyProj.Models.Order
....
<div>@Model.CustomerId<div>
....
解决方案 When you never load the Order, it can't be displayed.
EntityFramework Core do not support lazy loading yet. So when you call
Customer mycustomer = _context.Customer
.Where(c=> c.CustomerId== selectedId).SingleOrDefault();
only customer will loaded. You have to eager load it with .Include
or using .Load
Customer mycustomer = _context.Customer
.Include(c => c.order) // Add this line to tell EF Core to load orders too
.Where(c=> c.CustomerId== selectedId).SingleOrDefault();
Or alternatively:
Customer mycustomer = _context.Customer
.Where(c=> c.CustomerId== selectedId).SingleOrDefault();
// do explicit loading async
await _context.Entry(mycustomer)
.Reference(c => c.order)
.LoadAsync();
// do explicit loading sync
_context.Entry(mycustomer)
.Reference(c => c.order)
.Load();
这篇关于子表模型在部分视图中始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!