MVC 简便购物车

效果图:

(一)controlles

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers

{
    
    public class ShopCartController : Controller
    {
        //
        // GET: /ShopCart/

        public ActionResult Index()//显示的主页面
        {
            if (Session["cart"] == null)//判断一下是否为空
            {
                ViewBag.Count = 0;
            }
            else
            {
                List<OrderDetails> temp = Session["cart"] as List<OrderDetails>;
                ViewBag.Count = temp.Count;
            }

            List<Fruit> list = new FruitBF().Select();
            return View(list);
        }
        public ActionResult ViewCart()//购物车的视图
        {
            if (Session["cart"] == null)
            {
                List<OrderDetails> temp = new List<OrderDetails>();
                Session["cart"] = temp;
            }

            List<OrderDetails> list = Session["cart"] as List<OrderDetails>;//判断一共花了多少钱
            decimal cost = list.Sum(p=>p.Count*p.FruitPrice).Value;//用水果的数量乘以水果的单价
            ViewBag.Sum = cost; 
            return View(list);
        }
     
        public ActionResult RemoveFromCart(string id)//删除
        {
            if (Session["cart"] == null)//当集合里没有数值的时候
            {
                List<OrderDetails> temp = new List<OrderDetails>();
                Session["cart"] = temp;
            }
            List<OrderDetails> list = Session["cart"] as List<OrderDetails>;

            var query = list.Where(p=>p.FruitCode == id);
            if (query.Count() > 0)//判断一下,如果里面有数值得话,就减去一个
            {
                OrderDetails data = query.First();
                if (data.Count > 1)
                {
                    data.Count--;
                }
                else
                {
                    list.Remove(data);
                }
                return RedirectToAction("ViewCart");//刷新一下新操作的页面
            }
            else
            {
                return RedirectToAction("ViewCart");
            }
        }
        public ActionResult Buy(string id)//购买
        {
            if (Session["cart"] == null)//点击购买上面累计一共买了几种水果
            {
                List<OrderDetails> temp = new List<OrderDetails>();
                Session["cart"] = temp;
            }
            List<OrderDetails> list = Session["cart"] as List<OrderDetails>;

            var query = list.Where(p=>p.FruitCode == id);
            if (query.Count() <= 0)
            {
                OrderDetails data = new OrderDetails();
                data.FruitCode = id;
                data.Count = 1;
                list.Add(data);
            }
            else
            {
                OrderDetails data = query.First();
                data.Count++;
            }

            return RedirectToAction("Index");
        }
    }
}
(二) models
{1}fruitBF

using
System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcApplication1.Models { public class FruitBF { private MyDBDataContext _Context = new MyDBDataContext(); public List<Fruit> Select() { return _Context.Fruit.ToList(); } } }
{2} OrderDetails

using
System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcApplication1.Models { public partial class OrderDetails { private MyDBDataContext _Context = new MyDBDataContext(); public string FruitName { get { var query = _Context.Fruit.Where(p=>p.Ids == this.FruitCode); if (query.Count() > 0) { return query.First().Name; } return ""; } } public decimal FruitPrice { get { var query = _Context.Fruit.Where(p => p.Ids == this.FruitCode); if (query.Count() > 0) { return query.First().Price.Value; } return 0; } } } }

 

 

 

(三) views

<1> index--主页
@using MvcApplication1.Models; @model List
<Fruit> @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> <div>你已经购买了 @ViewBag.Count 个商品</div> <table width="100%" border="1" > @foreach(Fruit data in Model ) { <tr> <td>@data.Name</td> <td>@data.Price</td> <td>@data.Numbers</td> <td>@Html.ActionLink("购买", "Buy", "ShopCart", new { id=data.Ids},null)</td> </tr> } </table> @Html.ActionLink("查看购物车","ViewCart"); </div> </body> </html>

效果图:



<2> viewcart-- 购物车页面
@using MvcApplication1.Models; @model List
<OrderDetails> @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>ViewCart</title> </head> <body> <h1>查看购物车</h1> <div> <ul> @foreach(OrderDetails item in Model) { <li> @item.FruitName ( 购买数量:@item.Count 个 单价: @item.FruitPrice ) @Html.ActionLink("删除", "RemoveFromCart", new { id= item.FruitCode}) </li> } </ul> <font color="red"> 您一共购买了 @ViewBag.Sum 元的商品 </font> @Html.ActionLink("提交订单","SubmitOrder") </div> </body> </html>

(四) 添加一个JS

效果图:

转载于:https://www.cnblogs.com/w-wz/p/4647492.html

在现代社会中,电商已经成为人们消费的主要途径。而对于这样的电商平台,基于MVC的应用程序设计模式是一种非常理想的架构模式。.NET网上零食商城MVC是一种运用了MVC设计模式的电商平台,其具有极高的可扩展性、可维护性和可测试性等优点。这样的架构模式能够提高开发效率,减少开发难度,使开发人员能够更容易地对代码进行维护、升级和扩展。 在应用MVC设计模式的.NET网上零食商城中,Model层的主要任务是负责数据的存储和处理,包括从数据库中读取数据、数据处理、数据缓存等。同时,在这一层中还应该实现相应的数据验证机制,以确保数据的安全性和可靠性。 View层主要负责数据的显示,包括商品展示、购物车、订单等操作。用户通过这一层可以进行商品浏览、购买、支付等操作。在这一层中,应该注重UI的设计,要让用户体验到更简便、便捷的操作流程,从而提高用户的满意度和忠诚度。 Controller层则是整个应用程序的调度中心,主要负责用户请求路由、业务处理等操作。在这一层中,应该根据用户的需求对业务逻辑进行处理,使整个应用程序能够更加高效的运行。 总结来说,.NET网上零食商城MVC整个应用程序的运行流程清晰、逻辑严密,可以减轻开发人员的负担,提高开发效率,使用户可以更方便地进行商品浏览、购买和支付等操作。同时,零食市场也是一个非常火爆的市场,这个电商平台的运作将会给大多数人带来便捷的体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值