找传奇、传世资源到传世资源站!

asp.net mvc 商城系统购物车例子

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

ASP.NET cms
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace MvcShoping.Controllers{ public class CartController : Controller { // // GET: /Cart/ static List<Models.Cart> crats = new List<Models.Cart>(); List<Models.Product> produs = new List<Models.Product>() { new Models.Product{Pid=1,PName="特步",Description="非一般",PNum=100,Price=9995.5,PtId=1,Image="~/Images/1.jpg",PDatetime="2015-9-23"}, new Models.Product{Pid=2,PName="李宁",Description="一切皆有可能",PNum=100,Price=525.5,PtId=2,Image="~/Images/2.jpg",PDatetime="2015-9-23"}, new Models.Product{Pid=3,PName="安踏",Description="永不止步",PNum=100,Price=6665.5,PtId=3,Image="~/Images/3.jpg",PDatetime="2015-9-23"} }; public ActionResult Index() { var cars = crats.Where(c => c.Mid == 1).ToList(); List<Models.Product> ps=new List<Models.Product>(); foreach (var item in cars) { ps.Add(produs.Where(p => p.Pid == item.Pid).SingleOrDefault()); } return View(ps); } /// <summary> /// 添加购物车 /// </summary> /// <returns></returns> [Authorize] public ActionResult AddToCart(int pid) { Models.Cart car = new Models.Cart(); car.Cid = 1; car.Amount = 1; car.Pid = pid; car.Mid = 1; //判断购物车是否存在这件商品 if (crats.Where(c => c.Mid == car.Mid && c.Pid == pid).Count() == 0) { crats.Add(car); } return Content("OK"); } /// <summary> /// 删除购物车 /// </summary> /// <returns></returns> [OutputCache(Duration=0)] public ActionResult Remove(int pid) { string[] pids = Request.Form.GetValues("pids[]"); string[] checks = Request.Form.GetValues("checks[]"); string[] counts = Request.Form.GetValues("count[]"); crats.Remove(crats.Where(s => s.Pid == pid).SingleOrDefault()); var cras = crats.Where(c => c.Mid == 1).ToList(); List<Models.Product> ps = new List<Models.Product>(); if (pids != null) { foreach (var item in pids) { ps.Add(produs.Where(p => p.Pid.ToString() == item).SingleOrDefault()); } ViewBag.Check = checks.ToList(); ViewBag.counts = counts.ToList(); //从删除中剔除购物车中的记录,如果存在的话 List<Models.Cart> carss = (List<Models.Cart>)Session["carts"]; foreach (var item in carss) { if (item.Pid == pid) { carss.Remove(item); break; } } Session["carts"] = carss; } return PartialView("_PartialPage1", ps); } public ActionResult Ajaxplay() { string[] pids= Request.Form.GetValues("pid[]"); string[] counts = Request.Form.GetValues("count[]"); string[] prices = Request.Form.GetValues("price[]"); int sun = 0; double price = 0; if (counts == null) { return Content("0|0"); } for (int i = 0; i < pids.Length; i ) { price = produs.Where(d => d.Pid == int.Parse(pids[i])).SingleOrDefault().Price * double.Parse(counts[i]); sun = int.Parse(counts[i]); } return Content(price.ToString() "|" sun.ToString()); } public ActionResult Aajax(string pid) { var resule = produs.Where(p => p.Pid == int.Parse(pid)).SingleOrDefault().Price; return Content(resule.ToString()); } public ActionResult SS() { string[] pids = Request.Form.GetValues("pids[]"); string[] checks = Request.Form.GetValues("checks[]"); string[] counts = Request.Form.GetValues("count[]"); int sum = 0; int i = 0; List<Models.Cart> carts = new List<Models.Cart>(); foreach (var item in pids) { sum = int.Parse(item); Models.Cart c = new Models.Cart(); c.Pid = int.Parse(item); c.Amount = int.Parse(counts[i]); c.Mid = 1; carts.Add(c); i ; } Session["carts"] = carts; //保存 return Content(sum.ToString()); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复