webform中 ajax调用后台方法(非webservice)

后台:

 public partial class Ajax_ShoppingCart : System.Web.UI.Page
    {
        bookdbDataContext bdc = Connect.bookdb();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if(Request.Form["method"]!=null)
                {
                    switch (Request.Form["method"].ToString())
                    {
                        //添加商品至购物车
                        case "addToCart":
                            Response.Write(addToCart());
                            break;
                        //获得购物车
                        case "showCart":
                            Response.Write(showCart());
                            break;
                        //从购物车中删除商品
                        case "removeCart":
                            Response.Write(removeCart());
                            break;
                        //减少购物车中某一商品的数量
                        case "redCount":
                            Response.Write(redCount());
                            break;
                        //增加购物车中某一商品的数量
                        case "addCount":
                            Response.Write(addCount());
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        protected string tid
        {
            get
            {
                if (Request.Form["tid"] != null)
                {
                    return Request.Form["tid"].ToString();
                }
                else
                {
                    return "";
                }
            }
        }
        /// <summary>
        /// 添加至购物车
        /// </summary>
        /// <returns></returns>
        protected string addToCart()
        {
            int Count = 0;
            string message = string.Empty;
            if (Request.Form["count"] != null)
            {
                Int32.TryParse(Request.Form["count"].ToString(), out Count);
            }
            if (Count > 5 || Count < 1)
            {
                return JsonConvert.SerializeObject(new { status = 0, message = "书籍数量不对,不能超过5张哦" });
            }
            var tic = bdc.Book.SingleOrDefault(obj => obj.ID == Convert.ToInt32(tid));
            if (tic != null)
            {
                Common.Book t = new Common.Book();
                t.ID = tid;
                t.Count = Count;
                t.Name = tic.Name;
                t.Price = tic.Price;
                int result = ShoppingCartHelper.addCart(t);
                switch (result)
                {
                    case -2:
                        return JsonConvert.SerializeObject(new { status = 0, message = "同一书籍不能购买超过5本哦!" });
                    case 1:
                        return JsonConvert.SerializeObject(new { status = 1, cart = ShoppingCartHelper.getShoppingCart(), message = "成功加入购物车!" });
                    default:
                        return null;
                }
            }
            else
            {
                return JsonConvert.SerializeObject(new { status = 0, message = "不存在此书籍,请您重新选择!" });
            }
        }

        /// <summary>
        /// 获得购物车
        /// </summary>
        /// <returns></returns>
        protected string showCart()
        {
            return JsonConvert.SerializeObject(new { cart = ShoppingCartHelper.getShoppingCart() });
        }

        /// <summary>
        /// 从购物车中移除商品
        /// </summary>
        /// <returns></returns>
        protected string removeCart()
        {
            int result = ShoppingCartHelper.removeCart(tid);
            switch (result)
            {
                case -1:
                    return JsonConvert.SerializeObject(new { status = 0, message = "离开太久啦,购物车已经空空了,您重新购买吧" });
                default:
                    return JsonConvert.SerializeObject(new { status = 1, cart = ShoppingCartHelper.getShoppingCart() });
            }
        }

        /// <summary>
        /// 减少购物车中某一件商品的数量
        /// </summary>
        /// <returns></returns>
        protected string redCount()
        {
            int result = ShoppingCartHelper.reduceCart(tid);
            switch (result)
            {
                case -1:
                    return JsonConvert.SerializeObject(new { status = 0, message = "离开太久啦,购物车已经空空了,您重新购买吧" });
                default:
                    return JsonConvert.SerializeObject(new { status = 1, cart = ShoppingCartHelper.getShoppingCart() });
            }
        }

        /// <summary>
        /// 增加购物车中某一件商品的数量
        /// </summary>
        /// <returns></returns>
        protected string addCount()
        {
            int result = ShoppingCartHelper.addCart(tid);
            switch (result)
            {
                case -1:
                    return JsonConvert.SerializeObject(new { status = 0, message = "离开太久啦,购物车已经空空了,您重新购买吧" });
                default:
                    return JsonConvert.SerializeObject(new { status = 1, cart = ShoppingCartHelper.getShoppingCart() });
            }
        }
    }
后台代码

 

前台:

function ShowNewList(d,t) {
    var _url = "/Data_ajax/Ajax_ShoppingCart.aspx";//Data_ajax只是一个文件夹名字,随便怎么取,但是,该页面的aspx界面中,不能出现空格和内容,就是一个空的界面什么对没有
    var obj = {
        method: "addToCart",
        Id: d //在后台可以用Reques.Form["Id"]获取参数值
    };
    jQuery.ajax({
        type: "POST",
        url: _url,
        data: obj,
        dataType: "json",
        success: function (x) {
            ShowNewList1(x);
        }
    });
}

 

购物车类:

namespace Ipow.CMS.Helper
{
    public static class ShoppingCartHelper
    {
        /// <summary>
        /// 获取购物车
        /// </summary>
        /// <returns></returns>
        public static shoppingCart getShoppingCart()
        {
            shoppingCart cart = new shoppingCart();
            if (HttpContext.Current.Session["cart"] != null)
            {
                cart = HttpContext.Current.Session["cart"] as shoppingCart;
            }
            return cart;
        }

        /// <summary>
        /// 加入购物车
        /// </summary>
        /// <param name="tic">门票</param>
        /// <param name="CategoryID">门票类别</param>
        /// <returns></returns>
        public static int addCart(ticket tic, int CategoryID)
        {
            int result = 1;
            shoppingCart cart = getShoppingCart();
            if (cart.CategoryID == 0)
            {
                cart.CategoryID = CategoryID;
                cart.TotalPrice = tic.ticketPrice * tic.ticketCount;
                List<ticket> list = new List<ticket>();
                list.Add(tic);
                cart.tickets = list;
            }
            else
            {
                if (cart.CategoryID != CategoryID && cart.tickets.Count() != 0)
                {
                    //不同园区门票不能加入同一个购物车
                    result = -1;
                }
                else
                {
                    ticket t1 = cart.tickets.SingleOrDefault(obj => obj.ticketID == tic.ticketID);
                    //如果已存在同样的门票,则增加门票数量
                    if (t1 != null)
                    {
                        if (t1.ticketCount + tic.ticketCount <= 5)
                        {
                            t1.ticketCount += tic.ticketCount;
                            cart.TotalPrice += tic.ticketPrice * tic.ticketCount;
                            cart.CategoryID = CategoryID;
                        }
                        else
                        {
                            //门票数量超过5张
                            result = -2;
                        }
                    }
                    //不存在则添加
                    else
                    {
                        cart.TotalPrice += tic.ticketPrice * tic.ticketCount;
                        cart.CategoryID = CategoryID;
                        cart.tickets.Add(tic);
                    }
                }
            }
            HttpContext.Current.Session["cart"] = cart;
            return result;
        }

        /// <summary>
        /// 从购物车中移除商品
        /// </summary>
        /// <param name="tic">要移除的商品ID</param>
        /// <returns></returns>
        public static int removeCart(string tid)
        {
            shoppingCart cart = getShoppingCart();
            if (cart.CategoryID == 0)
            {
                //离开太久啦,购物车已经空空了,您重新购买吧
                return -1;
            }
            else
            {
                ticket t1 = cart.tickets.SingleOrDefault(obj => obj.ticketID == tid);
                if (t1 != null)
                {
                    //重新计算总价
                    cart.TotalPrice -= t1.ticketPrice * t1.ticketCount;
                    //从购物车中移除
                    cart.tickets.Remove(t1);
                }
            }
            HttpContext.Current.Session["cart"] = cart;
            return 1;
        }

        /// <summary>
        /// 减少购物车中某一商品的数量
        /// </summary>
        /// <param name="tic">要减少数量的商品id</param>
        /// <returns></returns>
        public static int reduceCart(string tid)
        {
            shoppingCart cart = getShoppingCart();
            if (cart.CategoryID == 0)
            {
                //离开太久啦,购物车已经空空了,您重新购买吧
                return -1;
            }
            else
            {
                ticket t1 = cart.tickets.SingleOrDefault(obj => obj.ticketID == tid);
                if (t1 != null)
                {
                    if (t1.ticketCount > 1)
                    {
                        //从购物车中减少某一商品的数量
                        t1.ticketCount--;
                        //重新计算总价
                        cart.TotalPrice -= t1.ticketPrice;
                    }
                }
            }
            HttpContext.Current.Session["cart"] = cart;
            return 1;
        }

        /// <summary>
        /// 增加购物车中某一商品的数量
        /// </summary>
        /// <param name="tic">要减少数量的商品id</param>
        /// <returns></returns>
        public static int addCart(string tid)
        {
            shoppingCart cart = getShoppingCart();
            if (cart.CategoryID == 0)
            {
                //离开太久啦,购物车已经空空了,您重新购买吧
                return -1;
            }
            else
            {
                ticket t1 = cart.tickets.SingleOrDefault(obj => obj.ticketID == tid);
                if (t1 != null)
                {
                    if (t1.ticketCount < 5)
                    {
                        //从购物车中减少某一商品的数量
                        t1.ticketCount++;
                        //重新计算总价
                        cart.TotalPrice += t1.ticketPrice;
                    }
                }
            }
            HttpContext.Current.Session["cart"] = cart;
            return 1;
        }
    }
    /// <summary>
    /// 购物车
    /// </summary>
    public class shoppingCart
    {
        /// <summary>
        /// 门票类别ID
        /// </summary>
        public int CategoryID { get; set; }
        /// <summary>
        /// 总价
        /// </summary>
        public decimal? TotalPrice { get; set; }
        /// <summary>
        /// 门票列表
        /// </summary>
        public List<ticket> tickets { get; set; }
    }
    /// <summary>
    /// 门票
    /// </summary>
    public class ticket
    {
        public string ticketID { get; set; }
        /// <summary>
        /// 门票名称
        /// </summary>
        public string ticketName { get; set; }
        /// <summary>
        /// 门票数量
        /// </summary>
        public int ticketCount { get; set; }
        /// <summary>
        /// 门票价格
        /// </summary>
        public decimal? ticketPrice { get; set; }
    }
}
购物车类

 

webform中 ajax调用后台方法(非webservice),古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。