mirror of
https://github.com/rudollee/LearningMVC.git
synced 2025-06-07 16:06:21 +00:00
34 lines
758 B
C#
34 lines
758 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Web.Mvc;
|
|
using SportsStore.Domain.Entities;
|
|
|
|
namespace SportsStore.WebUI.Infrastructure.Binders
|
|
{
|
|
public class CartModelBinder : IModelBinder
|
|
{
|
|
private const string sessionKey = "Cart";
|
|
|
|
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
|
|
{
|
|
Cart cart = null;
|
|
if (controllerContext.HttpContext.Session != null)
|
|
{
|
|
cart = (Cart)controllerContext.HttpContext.Session[sessionKey];
|
|
}
|
|
|
|
if (cart == null)
|
|
{
|
|
cart = new Cart();
|
|
if (controllerContext.HttpContext.Session != null)
|
|
{
|
|
controllerContext.HttpContext.Session[sessionKey] = cart;
|
|
}
|
|
}
|
|
|
|
return cart;
|
|
}
|
|
}
|
|
} |