I need some help. I have written code in MVC 5 .NET Framework where I used session to store my cart items list. It was pretty straight forward but now I am rewriting the code in ASP.NET MVC CORE 6 for another project but it is not working the way I was expecting. Scenario: I have a List of items as below:
private List<SalesCartItemsModel>? itemsList; //Shopping Cart
public SalesController()
{
itemsList = new List<SalesCartItemsModel>();
}
I want to use Session to store my Cart Items. I used this simple syntax:
Session["cartItems"] = null;
if (Session["cartItems"] != null)
{
itemsList = (List<PurchaseCartItem>)Session["cartItems"];
}
Session["cartItems"] = itemsList;
Now when i try to use the same in MVC CORE 6, it seems it doesnt support this syntax anymore. What i have tried so far is
HttpContext.Session.SetString("cartItems", itemsList)
But it throws error and does not accept it. ERROR:
cannot convert from 'System.Collections.Generic.List<.Models.SalesCart.SalesCartItemsModel>' to 'string'
Please help me how can I achieve the same in MVC Core? How can I change the code in the current syntax to MVC CORE 6.0?
CodePudding user response:
Maybe this is what you want to know:
Models:
public class SalesCartItemsModel
{
public string ShopId { get; set; }
public string ShopName { get; set; }
}
public class PurchaseCartItem
{
public string ShopId { get; set; }
public string ShopName { get; set; }
}
SessionExtensions:
public static class SessionExtensions
{
public static void Set<T>(this ISession session, string key, T value)
{
session.SetString(key, JsonSerializer.Serialize(value));
}
public static T Get<T>(this ISession session, string key)
{
var value = session.GetString(key);
return value == null ? default : JsonSerializer.Deserialize<T>(value);
}
}
Program:
builder.Services.AddSession();
app.UseSession();
Controller:
private List<SalesCartItemsModel>? itemsList;
private List<PurchaseCartItem>? itemListP;
public JsonResult Test()
{
itemsList = new List<SalesCartItemsModel>();
itemsList.Add(new SalesCartItemsModel { ShopId = "1", ShopName = "Apple" });
itemsList.Add(new SalesCartItemsModel { ShopId = "2", ShopName = "Banana" });
HttpContext.Session.Set<List<SalesCartItemsModel>>("cartItems", itemsList);
var value = HttpContext.Session.Get<List<SalesCartItemsModel>>("cartItems");
return Json(value);
}
Result:
When you don't store the value in the session, you can judge value=null
to store the new value for the session:
public JsonResult Test()
{
itemsList = new List<SalesCartItemsModel>();
//itemsList.Add(new SalesCartItemsModel { ShopId = "1", ShopName = "Apple" });
//itemsList.Add(new SalesCartItemsModel { ShopId = "2", ShopName = "Banana" });
//HttpContext.Session.Set<List<SalesCartItemsModel>>("cartItems", itemsList);
var value = HttpContext.Session.Get<List<SalesCartItemsModel>>("cartItems");
if (value == null)
{
itemListP = new List<PurchaseCartItem>();
itemListP.Add(new PurchaseCartItem { ShopId = "1", ShopName = "Orange" });
itemListP.Add(new PurchaseCartItem { ShopId = "2", ShopName = "Pear" });
HttpContext.Session.Set<List<PurchaseCartItem>>("cartItems", itemListP);
var valueP = HttpContext.Session.Get<List<PurchaseCartItem>>("cartItems");
return Json(valueP);
}
else
{
return Json(value);
}
}
Result:
When there is no value in your itemsList
, you can judge value.Count=0
to add a new value to the session:
public JsonResult Test()
{
itemsList = new List<SalesCartItemsModel>();
//itemsList.Add(new SalesCartItemsModel { ShopId = "1", ShopName = "Apple" });
//itemsList.Add(new SalesCartItemsModel { ShopId = "2", ShopName = "Banana" });
HttpContext.Session.Set<List<SalesCartItemsModel>>("cartItems", itemsList);
var value = HttpContext.Session.Get<List<SalesCartItemsModel>>("cartItems");
if (value.Count == 0)
{
itemListP = new List<PurchaseCartItem>();
itemListP.Add(new PurchaseCartItem { ShopId = "1", ShopName = "Orange" });
itemListP.Add(new PurchaseCartItem { ShopId = "2", ShopName = "Pear" });
HttpContext.Session.Set<List<PurchaseCartItem>>("cartItems", itemListP);
var valueP = HttpContext.Session.Get<List<PurchaseCartItem>>("cartItems");
return Json(valueP);
}
else
{
return Json(value);
}
}
Result: