Я хочу добавить список объектов в сеанс. Но в моем коде всегда создается свежий объект (всегда только 1 элемент в списке)
DTO
public class CartTotal
{
public decimal SubTotal { get; set; }
public decimal DeliveryCharges { get; set; }
public decimal GrandTotal { get; set; }
public int CurrentItemCount { get; set; }
public List<Items> items { get; set; }
}
Предметы
public class Items
{
public int ItemId { get; set; }
public string ItemCode { get; set; }
public string ItemName { get; set; }
public string ImageUrl { get; set; }
public int? ItemBadge { get; set; }
public DateTime? AddedDate { get; set; }
public int? AddedBy { get; set; }
}
Session
public CartTotal ItemsHolder
{
get
{
object ItemsSession = Session["ItemSession"] as CartTotal;
if (ItemsSession == null)
{
ItemsSession = new CartTotal();
Session["ItemSession"] = ItemsSession;
}
return (CartTotal)ItemsSession;
}
}
Я пытался добавить элементы в список, как показано ниже. Но всегда добавлялся свежий элемент и (всегда только 1 элемент в списке.)
ItemsHolder.items = new List<Items>(); // When i comment this line,below code not working.
ItemsHolder.items.Add(new Items() {
ItemId = items.ItemId,
ItemName = items.ItemName,
ItemPrice = items.ItemPrice,
ImageUrl = items.ImageUrl,
ItemCode = items.ItemCode
});