因此该项目在.Net Framework中可以正常工作,但现在我正在尝试在.Net Core 3中进行操作,但无法正常工作。
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string ImageURL { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
}
public class BasketLine
{
public int Id { get; set; }
public string BasketId { get; set; }
public int ProductId { get; set; }
public int Quantity { get; set; }
public DateTime DateCreated { get; set; }
public Product Product { get; set; }
}
那么我将可以添加到BasketLine表并计算所有产品的总数,但是当我加载购物车页面时,我得到了NullReferenceException
在购物车页面上,如果我注释掉@Model.BasketLines[i].Product.Name
或任何导航属性,则购物车页面有效
这是addBasket方法
public void AddToBasket(int productId, int quantity)
{
var basketLine = db.BasketLines.FirstOrDefault(b => b.BasketId == BasketId &&
b.ProductId == productId);
if (basketLine == null)
{
basketLine = new BasketLine
{
ProductId = productId,
BasketId = BasketId,
Quantity = quantity,
DateCreated = DateTime.Now
};
db.BasketLines.Add(basketLine);
}
else
{
basketLine.Quantity += quantity;
}
db.SaveChanges();
}
我错过了一些东西是因为我不知道我在做什么错
添加了Include并没有改变任何东西,然后发生了一件奇怪的事情,我改变了这个
<a asp-area="Customer"
asp-controller="Shop"
asp-action="Details"
asp-route-id="@Model.BasketLines[i].ProductId">
@Model.BasketLines[i].Product.Name
</a>
对此
@Html.ActionLink(Model.BasketLines[i].Product.Name, "Details", "Products", new { id = Model.BasketLines[i].ProductId }, null)<br />
然后一切都尝试了几次,然后我又一次遇到了相同的错误
AspNetCore.Areas_Customer_Views_Basket_Index.<ExecuteAsync>b__15_0() in Index.cshtml
+
@Html.ActionLink(Model.BasketLines[i].Product.Name, "Details", "Products", new { id = Model.BasketLines[i].ProductId }, null)<br />
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.GetChildContentAsync(bool useCachedResult, HtmlEncoder encoder)
Microsoft.AspNetCore.Mvc.TagHelpers.RenderAtEndOfFormTagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.<RunAsync>g__Awaited|0_0(Task task, TagHelperExecutionContext executionContext, int i, int count)
AspNetCore.Areas_Customer_Views_Basket_Index.ExecuteAsync() in Index.cshtml
+
{
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, string contentType, Nullable<int> statusCode)
答案 0 :(得分:0)
您尝试过function displayCart() {
var cartArray = sessionStorage("shoppingCart").listCart();
...
吗?您可以使用Include方法来指定要包含在查询结果中的相关数据。
Include