【发布时间】:2020-11-04 21:25:18
【问题描述】:
我正在尝试在用户购物篮中显示品牌名称、产品颜色和产品尺寸。我在这个项目中使用 ASP.NET Core 3.1。
我将此查询与include 一起使用:
public IEnumerable<UserBasket> GetUserBasket(int userId)
{
return _context.UserBaskets
.Include(p=>p.Product)
.Include(b=>b.ProductBrand)
.Include(s=>s.ProductSize)
.Include(c=>c.ProductColor)
.Where(u=>u.UserId==userId)
.ToList();
}
这就是行动:
public IActionResult ShowOrders()
{
var userId = _userService.GetUserIdByUserName(User.Identity.Name);
return View(_orderService.GetUserBasket(userId));
}
这是动作的视图:
我用表格来展示用户购物篮
@model IEnumerable<UserBasket>
@foreach (var item in Model)
{
<tr class="gradeA odd">
<td class="sorting_1">@item.Product.Name</td>
<td class="center ">@item.Count</td>
<td class="center ">@item.SumPrice</td>
<td class="center ">@item.CreateDate.Value.ToShamsi()</td>
<td class="center ">@item.ProductBrand.BrandName</td>
<td class="center ">@item.Size</td>
<td class="center ">@item.Color</td>
@if (item.IsFinally)
{
<td class="center "><i class="glyphicon-ok"></i></td>
}
else
{
<td class="center "><i class="glyphicon-remove"></i></td>
}
</tr>
}
这是 UserBasket 类:
public class UserBasket
{
public UserBasket()
{
}
[Key]
public int UB_Id { get; set; }
public int UserId { get; set; }
public int ProductId { get; set; }
public int PB_Id { get; set; }
public int PC_Id { get; set; }
public int PS_Id { get; set; }
public int Count { get; set; }
public int SumPrice { get; set; }
public bool IsFinally { get; set; }
public DateTime? CreateDate { get; set; }
[Required]
[MaxLength(200)]
public string BrandName { get; set; }
[Required]
[MaxLength(200)]
public string Color { get; set; }
[Required]
[MaxLength(200)]
public string Size { get; set; }
#region relations
public virtual User.User User { get; set; }
public virtual Product.Product Product { get; set; }
public virtual ProductBrand ProductBrand { get; set; }
public virtual ProductColor ProductColor { get; set; }
public virtual ProductSize ProductSize { get; set; }
#endregion
}
这是产品类:
public class Product
{
public Product()
{
}
[Key]
public int ProductId { get; set; }
public int CategoryId { get; set; }
public int PB_Id { get; set; }
public int PC_Id { get; set; }
public int PS_Id { get; set; }
public int Price { get; set; }
public bool IsAvailable { get; set; }
public string Name { get; set; }
public string ShortDescription { get; set; }
public string Description { get; set; }
public string ImageName { get; set; }
public DateTime? CreateDate { get; set; }
public bool? IsDeleted { get; set; }
public int? Visit { get; set; }
#region relations
public virtual Category Category { get; set; }
public virtual ProductBrand ProductBrand { get; set; }
public virtual ProductColor ProductColor { get; set; }
public virtual ProductSize ProductSize { get; set; }
public virtual List<UserBasket> UserBaskets { get; set; }
#endregion
}
这是用户类:
public class User
{
public User()
{
}
[Key]
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string ActiveCode { get; set; }
public bool IsActive { get; set; }
public DateTime? RegisterDate { get; set; }
public bool? IsDeleted { get; set; }
#region relations
public virtual List<UserBasket> UserBaskets { get; set; }
public virtual List<UserOrder> UserOrders { get; set; }
#endregion
}
当我运行它显示的项目时
System.NullReferenceException: '对象引用未设置为对象的实例。'
在视图中。我该怎么办?
【问题讨论】:
-
你能告诉我们 UserBasket 类吗?
-
我用 UserBasket 类编辑了问题。
标签: c# entity-framework asp.net-core .net-core entity-framework-core