I'm trying to get SubCategory after Include(p => p.SubCategory) and get the error:
"Newtonsoft.Json.JsonSerializationException: 'Self referencing loop detected with type 'E_Store2021.Models.Product'. Path '[0].Product.SubCategory.Products' "
I need to get SubCategoryName. What should I do in this case? Without Include() everything works fine. The same will happen with the company.
Markup:
<tr>
<td>
<figure class="itemside align-items-center">
<div class="aside"><img src="@("~/images/content/" + item.Product.ImagePath)" asp-append-version="true" class="img-sm"/></div>
<figcaption class="info">
<a href="#" class="title text-dark" data-abc="true">@item.Product.ProductName</a>
<p class="text-muted small">Category: <br> Brand: @item?.Product?.Company.CompanyName</p>
<p class="text-muted small">SubCategory: @item.Product?.SubCategory?.SubCategoryName</p>
</figcaption>
</figure>
</td>
<td>
<select class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</td>
<td>
<div class="price-wrap"> <var class="price">@item.Product.UnitPrice</var> <small class="text-muted"> $9.20 each </small> </div>
</td>
<td class="text-right d-none d-md-block"> <a data-original-title="Save to Wishlist" title="" href="" class="btn btn-light" data-toggle="tooltip" data-abc="true"> <i class="fa fa-heart"></i></a> <a href="" class="btn btn-light" data-abc="true"> Remove</a> </td>
</tr>
Code:
namespace E_Store2021.Controllers
{
public class CartController : Controller
{
private ApplicationDbContext _context;
public CartController(ApplicationDbContext context)
{
_context = context;
}
[AllowAnonymous]
public IActionResult Index()
{
var cart = SessionHelper.GetObjectFromJson<List<ShoppingCartItem>>(HttpContext.Session, "cart");
ShoppingCartModel.ShoppingCartItems = cart;
ShoppingCartModel.Total = cart?.Sum(item => item.Product.UnitPrice * item.Quantity);
return View();
}
[AllowAnonymous]
public IActionResult Buy(int id)
{
if (SessionHelper.GetObjectFromJson<List<ShoppingCartItem>>(HttpContext.Session, "cart") == null)
{
List<ShoppingCartItem> cart = new List<ShoppingCartItem>();
cart.Add(new ShoppingCartItem { Product = _context.Products.Include(p=>p.SubCategory).FirstOrDefault(p => p.ProductID == id), Quantity = 1 });
SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
}
else
{
List<ShoppingCartItem> cart = SessionHelper.GetObjectFromJson<List<ShoppingCartItem>>(HttpContext.Session, "cart");
int index = IsExist(id);
if (index != -1)
cart[index].Quantity++;
else
cart.Add(new ShoppingCartItem { Product = _context.Products.Include(p => p.SubCategory).FirstOrDefault(p => p.ProductID == id), Quantity = 1 });
SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
}
return RedirectToAction("Index");
}
[AllowAnonymous]
public IActionResult Remove(int id)
{
List<ShoppingCartItem> cart = SessionHelper.GetObjectFromJson<List<ShoppingCartItem>>(HttpContext.Session, "cart");
int index = IsExist(id);
cart.RemoveAt(index);
SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
return RedirectToAction("Index");
}
[AllowAnonymous]
private int IsExist(int id)
{
List<ShoppingCartItem> cart = SessionHelper.GetObjectFromJson<List<ShoppingCartItem>>(HttpContext.Session, "cart");
for (int i = 0; i < cart.Count; i++)
{
if (cart[i].Product.ProductID.Equals(id))
return i;
}
return -1;
}
}
}
SessionHelper. An error occurs here when trying to serialize an object.
public static class SessionHelper
{
public static void SetObjectAsJson(this ISession session, string key, object value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
public static T GetObjectFromJson<T>(this ISession session, string key)
{
var value = session.GetString(key);
return value == null ? default : JsonConvert.DeserializeObject<T>(value);
}
}