[asp.net]异步提交数据库实现简单购物车

最近在做一个商场系统,分享一下自己做的购物车

购物车是用数据库实现的,当然也能用cookie实现

项目是基于三层实现,学生党表示还不会MVC

大概的思想就是----先读取出用户数据库中的购物车的商品,输出到前台,前台如果有购物车数量修改就通过异步提交给一般处理程序完成数量的更新

刚刚接触异步,大佬轻喷

Carts.cs

 1 public class Carts
 2     {
 3                 
 4           /// <summary>
 5         /// 购物车ID
 6         /// </summary>
 7         public int cartId { get; set; }
 8         /// <summary>
 9         /// 用户ID
10         /// </summary>
11         public int userId { get; set; }
12         /// <summary>
13         /// 产品ID
14         /// </summary>
15         public int productId { get; set; }
16         /// <summary>
17         /// 产品数量
18         /// </summary>
19         public int productCount { get; set; }
20         /// <summary>
21         /// 添加时间
22         /// </summary>
23         public DateTime addTime { get; set; }
24            
25     }

前台页面

Cart.aspx

 1    <table width="100%" border="0" id="tab">
 2                         <tbody>
 3                             <tr>
 4 
 5                                 <td class="tr-title">商品名称</td>
 6                                 <td class="tr-title">积分</td>
 7                                 <td class="tr-title">金额</td>
 8                                 <td class="tr-title">优惠</td>
 9                                 <td class="tr-title">数量</td>
10                                 <td class="tr-title">操作</td>
11                             </tr>
12                             <% foreach (var item in CartList)
13                                 { %>
14                             <tr>
15 
16                                 <td class="tr-list">
17                                     <a href="detail.aspx?item=<%=item.productId %>">
18                                         <img class="pull-left" alt="" src="<%= item.productIMG %>" />
19                                         <div class="summary blue-font vm"><%= item.productName %></div>
20                                     </a>
21                                 </td>
22                                 <td class="tr-list"><b><%=item.productStone %></b></td>
23                                 <td class="tr-list">¥<b class="orange-font"><%= item.productPrice.ToString("0.00") %></b>
24                                 </td>
25                                 <td class="tr-list">减¥0.00</td>
26                                 <td class="tr-list">
27                                     <span class="ui-spinner">
28                                         <input class="text_box" type="text" value="<%= item.productCount %>" autocomplete="off" οnkeyup="CheckValue(this)" οnblur="setTotal();update(this,<%=item.cartId %>,<%=item.userId %>,this.value);">
29                                         <%-- href="/handler/cartUpdate.ashx?id=<%=item.cartId %>&user=<%=item.userId %>&count=<%=item.productCount%>&type=add" --%>
30                                         <a class="ui-spinner-button ui-spinner-up add" tabindex="-1" οnclick="update(this,<%=item.cartId %>,<%=item.userId %>,<%=item.productCount %>,'add')"></a>
31                                         <a class="ui-spinner-button ui-spinner-down reduce" tabindex="-1" οnclick="update(this,<%=item.cartId %>,<%=item.userId %>,<%=item.productCount %>,'reduce')"></a>
32                                     </span>
33                                 </td>
34                                 <td width="8%" class="tr-list bule-font"><a href="/handler/DeleteCart.ashx?item=<%=item.cartId%>" οnclick="return confirm('确定删除?')">删除</a></td>
35                             </tr>
36                             <% } %>
37                         </tbody>
38                     </table>
39  <div class="col-lg-12 main-show mb10">
40                 <div class="p10">
41 
42                     <div class="show-right pull-right tr">
43                         <div class=""><b class="totalCount"></b>件商品 总计:¥<span class="totalPrice"></span></div>
44                         <div class="">返现:-¥0.00</div>
45                     </div>
46                     <div class="clearfix"></div>
47                 </div>
48                 <div class="total tr"><b>总计(不含运费):</b> <i class="orange-font f20 tit-family pr10">¥<b class="totalPrice"></b></i></div>
49 
50             </div>
51             <div class="pull-right">
52                 <a href="javascript:history.go(-1)" type="button" class="btn btn-addcart btn-lg mr20">继续购物</a>
53                 <a href="confirm.aspx?from=cart" type="button" class="btn btn-danger btn-lg mr20">去结算</a>
54             </div>
前台

前台jquery脚本

 1 //防止数量文本框内有非数字值
 2         function CheckValue(obj) {
 3             var v = obj.value.replace(/[^\d]/g, '');
 4             if (v == '' || v == 'NaN') {
 5                 obj.value = "1";
 6             }
 7             else {
 8                 obj.value = v;
 9             }
10         }
11 
12         //计算总金额
13         function setTotal() {
14             var sum = 0;
15             var count = 0;
16             $("#tab tr").each(function () {
17                 if (!isNaN(parseInt($(this).find('input[class*=text_box]').val()))) {
18                     sum += parseInt($(this).find('input[class*=text_box]').val()) * parseFloat($(this).find('.orange-font').text());
19                     count += parseInt($(this).find('input[class*=text_box]').val());
20                 }
21             });
22             $(".totalPrice").html(sum.toFixed(2));
23             $(".totalCount").html(count);
24 
25         }
26         setTotal();
27 
28         //异步更新购物车
29         function update(obj, cid, user, pcount, select) {
30             var json = { id: cid, userid: user, count: pcount, type: select };
31             $.post("/handler/cartUpdate.ashx", json, function (data) {
32                 if (data > 0) {
33                     $(obj).parent().find("input[type=text]").val(data);
34                     setTotal();
35                 }
36             })
37         }
View Code

后台

Cart.aspx.cs

 1 protected IEnumerable<Shop.Model.CartInfo> CartList { get; set; }
 2 
 3         CartsBLL cartsBLL = new CartsBLL();
 4         CartInfoBLL InfoBLL = new CartInfoBLL();
 5         ProductsBLL productsBLL = new ProductsBLL();
 6         protected void Page_Load(object sender, EventArgs e)
 7         {
 8             //CookieHelper.Set("user_carts", "[{\"BookId\":\"4995\",\"Count\":\"1\"},{\"BookId\":\"4996\",\"Count\":\"2\"}]", DateTime.Now.AddDays(10));
 9             var item = Request["item"];
10             if (CurrentUser != null)
11             {
12                 if (!string.IsNullOrEmpty(item))
13                 {
14                     // 添加购物车
15                     AddCart(item.ToInt32());
16                 }
17 
18                 // 代码执行顺序
19                 CartList = InfoBLL.QueryList(-1, -1, new { userId = CurrentUser.userId }, "cartId", true);
20                 
21             }
22             else
23             {
24                 // 离线购物车
25             }
26         }
27 
28         private void AddCart(int item)
29         {
30             // 执行添加购物车逻辑
31             if (item == 0)
32             {
33                 NotFound();
34                 return;
35             }
36 
37             // 判断商品是否存在
38             var exist = productsBLL.QuerySingle(item);
39             if (exist == null)
40             {
41                 NotFound();
42                 return;
43             }
44             // 有商品 判断当前商品是否在购物车中存在
45             // 查询用户的购物车
46             var userCart = cartsBLL.QueryList(-1, -1, new { userId = CurrentUser.userId }, "cartId");
47             // 查询用户购物车是否存在该商品
48             var cartExist = userCart.ToList<Carts>().Find(p => p.productId == item);
49             if (cartExist == null)
50             {
51                 // 没有
52                 cartExist = new Shop.Model.Carts();
53                 cartExist.productId = item;
54                 cartExist.productCount = 1;
55                 cartExist.userId = CurrentUser.userId;
56                 cartExist.addTime = DateTime.Now;
57                 cartsBLL.Insert(cartExist);
58 
59             }
60             else
61             {
62                 //
63                 cartExist.productCount += 1;
64                 cartsBLL.Update(cartExist);
65             }
66 
67 
68         }
View Code

异步处理程序

CartUpdate.ashx

 1 public void ProcessRequest(HttpContext context)
 2         {
 3             context.Response.ContentType = "text/html";
 4             int cartid = context.Request["id"].ToInt32();
 5             int userid = context.Request["userid"].ToInt32();
 6             int count = context.Request["count"].ToInt32();
 7             string type = context.Request["type"];
 8 
 9             
10             BLL.CartsBLL cartsBLL = new BLL.CartsBLL();
11             Model.Carts cart = cartsBLL.QuerySingle(cartid);
12             if (cart == null)
13             {
14                 context.Response.Write("-1");
15                 context.Response.End();
16             }
17             if (type == "add")
18             {
19                 //执行添加操作
20                 cart.productCount++;
21             }
22             else if (type == "reduce")
23             {
24                 //执行减少操作
25                 if (cart.productCount > 1)
26                 {
27                     cart.productCount--;
28                 }
29                 else
30                 {
31                     context.Response.Write("-1");
32                     context.Response.End();
33                 }
34             }
35             else
36             {
37                 //将数量修改为文本框失去焦点时的数量
38                 if (count > 0)
39                 {
40                     cart.productCount = count;
41                 }
42 
43             }
44             cartsBLL.Update(cart);
45             context.Response.Write(cart.productCount);
46         }
CartUpdate.ashx

 

转载于:https://www.cnblogs.com/shoted/p/8489082.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值