[英]JsonConvert.SerializeObject throwing self referencing error
I'm working on a Pizza Store with shopping cart that uses session to store the cart and return it has a partial view to update the cart through AJAX.我正在使用带有购物车的比萨店工作,该购物车使用会话来存储购物车并返回它具有通过 AJAX 更新购物车的局部视图。
This method has worked perfectly until recently, I haven't made any specific changes to the method or the CartController itself.直到最近,此方法一直运行良好,我还没有对该方法或 CartController 本身进行任何特定更改。 But I have been working on the admin page to add more products, which is working as intended and shouldn't affect this part at all.
但是我一直在管理页面上添加更多产品,这按预期工作,根本不应该影响这部分。
I have used visual studio to step through the code, it calls the correct method and getting correct values, but it stops at this line var temp = JsonConvert.SerializeObject(cart);我已经使用 Visual Studio 来单步执行代码,它调用正确的方法并获取正确的值,但它停在这一行 var temp = JsonConvert.SerializeObject(cart); where it throws Failed to load resource: the server responded with a status of 500 () error which can be seen in the browser console.
它抛出 Failed to load resource: 服务器响应状态 500 () 错误,可以在浏览器控制台中看到。
Full path is:完整路径为:
[https://localhost:44341/cart/product/1?X-Requested-With=XMLHttpRequest&_=1549979834762]
[Route("product/{id}")]
public async Task<IActionResult> AddItem(int id)
{
var user = await _userManager.GetUserAsync(User);
if (user == null) Challenge();
ShoppingCart cart;
var product = await _dishService.GetDishAsync(id);
// If shopping cart doesn't exist
if (HttpContext.Session.GetString("varukorg") == null)
{
cart = new ShoppingCart
{
Products = new List<Matratt>(),
User = user,
Kund = await _userService.FindUserAsync(user.Id)
};
}
else
{
// Else get the session and cast it as cart
var serializedValue = ( HttpContext.Session.GetString("varukorg") );
cart = JsonConvert.DeserializeObject<ShoppingCart>(serializedValue);
}
cart.Products.Add(product);
// Added product, putting the session back.
var temp = JsonConvert.SerializeObject(cart);
HttpContext.Session.SetString("varukorg", temp);
return PartialView("_CartList", cart);
}
And the method is called from the view through unobtrusive AJAX, which has worked perfectly up until now.并且该方法是通过不显眼的 AJAX 从视图中调用的,到目前为止,该方法一直运行良好。
<a class="btn btn-sm btn-success"
data-ajax="true"
data-ajax-method="GET"
data-ajax-mode="replace"
data-ajax-update="#cartList"
asp-controller="Cart" asp-action="AddItem" asp-route-id="@item.Id">+
</a>
And the ViewModel used和使用的 ViewModel
public List<Matratt> Products { get; set; }
public AppUser User { get; set; }
public Kund Kund { get; set; }
public bool CalculatedPoints { get; set; }
When I requested the object from the service class I used a general method to get the navigation objects as well, which in this case seems to have been the cause for the self referencing error.当我从服务类请求对象时,我也使用了一个通用方法来获取导航对象,在这种情况下,这似乎是导致自引用错误的原因。
return await _context.Matratt
.Include(m => m.MatrattProdukt)
.ThenInclude(p => p.Produkt)
.FirstOrDefaultAsync(m => m.MatrattId == id);
Changed to only改为仅
return await _context.Matratt.FirstOrDefaultAsync(p => p.MatrattId == id);
Which is the only thing really needed for the cart.这是购物车唯一真正需要的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.