国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

[回饋]ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)(五)

這篇具有很好參考價值的文章主要介紹了[回饋]ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)(五)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

經(jīng)過一段時間的準備,新的一期【ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)】已經(jīng)開始,在之前的文章中,講解了商城系統(tǒng)的整體功能設(shè)計,頁面布局設(shè)計,環(huán)境搭建,系統(tǒng)配置,及首頁【商品類型,banner條,友情鏈接,降價促銷,新品爆款】,商品列表頁面,商品詳情等功能的開發(fā),今天繼續(xù)講解購物車功能開發(fā),僅供學習分享使用,如有不足之處,還請指正。

[回饋]ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)(五)

?

購物車功能說明

?

在首頁或者商品列表頁面,如果用戶對商品感興趣,可以點擊快捷方式,將商品加入購物車;或者在商品詳情頁面,選擇對應(yīng)的商品參數(shù)后,將商品加入購物車。商品加入購物車的渠道是有多種,而用戶也可以對已經(jīng)加入購物車的商品進行購買,或者刪除購物車。每一個用戶都有各自的購物車,相互之間獨立,所以購物車功能需要用戶先進行登錄,才能查看。

?

購物車功能設(shè)計

?

根據(jù)購物車功能說明,購物車主要顯示已添加的商品列表,并可以刪除,或者選擇商品進行購買,設(shè)計頁面如下所示:

[回饋]ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)(五)

?

購物車功能開發(fā)

?

?

購物車主要展示用戶選擇的商品信息。

?

1. 數(shù)據(jù)表創(chuàng)建

?

購物車表EB_Cart主要用于存儲商品信息,用戶信息,數(shù)量,及個人喜好等內(nèi)容,如下所示:

[回饋]ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)(五)

購物車表創(chuàng)建語句如下所示:

CREATE TABLE [dbo].[EB_Cart](
	[Id] [bigint] IDENTITY(1,1) NOT NULL,
	[ProductId] [bigint] NULL,
	[CustomerId] [bigint] NULL,
	[Quantity] [int] NULL,
	[Remark] [varchar](200) NULL,
	[CreateTime] [datetime] NULL,
	[CreateUser] [varchar](50) NULL,
	[LastEditTime] [datetime] NULL,
	[LastEditUser] [varchar](50) NULL
) ON [PRIMARY]

?

2. 購物車實體創(chuàng)建

?

購物車實體和數(shù)據(jù)表結(jié)構(gòu)保持一致,方便進行映射。如下所示:

using SqlSugar;

namespace EasyBuyShop.Models
{
    /// <summary>
    /// 購物車
    /// </summary>
    [SugarTable("EB_Cart")]
    public class Cart:EntityModel
    {
        public long ProductId { get; set; }

        public long CustomerId { get; set; }

        /// <summary>
        /// 數(shù)量
        /// </summary>
        public int Quantity { get; set; }

        public string Remark { get; set; }
    }
}

?

3. 數(shù)據(jù)處理層DAL

?

購物車列表,主要包括添加購物車,刪除,查詢等功能,DAL層代碼如下所示:

using EasyBuyShop.Models;
using EasyBuyShop.Utils;

namespace EasyBuyShop.DAL
{
    public class CartDal:BaseDal
    {
        /// <summary>
        /// 獲取購物車列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<Cart> GetCartListByUserId(long userId)
        {
            try
            {
                using (var db = this.GetDb(BaseDal.ConnStr))
                {
                    return db.Queryable<Cart>().Where(r => r.CustomerId == userId).ToList();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Fatal(ex.Message);
                return new List<Cart>();
            }
        }

        public int DeleteById(long id)
        {
            try
            {
                using (var db = this.GetDb(BaseDal.ConnStr))
                {
                    int cnt = db.Deleteable<Cart>(r => r.Id == id).ExecuteCommand();
                    return cnt;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Fatal(ex.Message);
                return -1;
            }
        }

        public Cart GetCart(long id)
        {
            try
            {
                using (var db = this.GetDb(BaseDal.ConnStr))
                {
                    return db.Queryable<Cart>().First(r => r.Id == id);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Fatal(ex.Message);
                return null;
            }
        }
    }
}

?

4. 控制器獲取

?

控制器方法主要包括添加購物車【1.首頁或商品列表快捷添加購物車 2.商品詳情頁面添加購物車】,查詢購物車, 刪除購物車,代碼如下所示:

using EasyBuyShop.DAL;
using EasyBuyShop.Models;
using Microsoft.AspNetCore.Mvc;

namespace EasyBuyShop.Controllers
{
    /// <summary>
    /// 購物車控制器
    /// </summary>
    public class CartController : Controller
    {
        /// <summary>
        /// 購物車列表頁面
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            
            var userId = HttpContext.Session.GetInt32("userid");
            if (userId == null)
            {
               return Redirect("/Auth/login");
            }
            var cartDal = new CartDal();
            var productDal = new ProductDal();
            var cartList = cartDal.GetCartListByUserId((long)userId);
            var products = productDal.GetProductListByIds(cartList.Select(r => r.ProductId).ToList());
            ViewData["CartList"] = cartList;
            ViewData["ProductList"]= products;
            var username = HttpContext.Session.GetString("username");
            var realName = HttpContext.Session.GetString("realname");
            ViewData["Username"] = username;
            ViewData["RealName"] = realName;
            return View();
        }

        /// <summary>
        /// 首頁或商品列表,快捷加入購物車
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add(int productId)
        {
            Msg msg = new Msg();
            var userId = HttpContext.Session.GetInt32("userid");
            var userName= HttpContext.Session.GetString("username");
            if (userId == null)
            {
                msg.code = -1;
                msg.message = "尚未登錄";
                return Json(msg);
            }
            var productDal = new ProductDal();
            var product = productDal.GetProduct(productId);
            if (product != null)
            {
                var cartDal = new CartDal();
                var cart=new Cart();
                cart.ProductId = productId;
                cart.CustomerId = userId.Value;
                cart.Quantity = 1;
                cart.Remark= string.Empty;
                cart.CreateUser = userName;
                cart.CreateTime=DateTime.Now;
                cart.LastEditUser = userName;
                cart.LastEditTime = DateTime.Now;
                int id = cartDal.InsertT<Cart>(cart);
                if(id > 0)
                {
                    msg.code = 0;
                    msg.message = "成功";
                    return Json(msg);
                }
                else
                {
                    msg.code = -1;
                    msg.message = "加入購物車失敗";
                    return Json(msg);
                }
            }
            else
            {
                msg.code = -1;
                msg.message = "商品不存在";
                return Json(msg);
            }
            
        }

        /// <summary>
        /// 商品詳情頁面加入購物車
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddWithForm()
        {
            Msg msg = new Msg();
            var userId = HttpContext.Session.GetInt32("userid");
            var userName = HttpContext.Session.GetString("username");
            if (userId == null)
            {
                msg.code = -1;
                msg.message = "尚未登錄";
                return Json(msg);
            }
            var productId =long.Parse( Request.Form["productId"]);
            var quantity = int.Parse(Request.Form["quantity"]);
            var color = Request.Form["color"];
            var size = Request.Form["size"];
            var remark = $"顏色:{color},大?。簕size}";
            var productDal = new ProductDal();
            var product = productDal.GetProduct(productId);
            if (product != null)
            {
                var cartDal = new CartDal();
                var cart = new Cart();
                cart.ProductId = productId;
                cart.CustomerId = userId.Value;
                cart.Quantity = quantity;
                cart.Remark = remark;
                cart.CreateUser = userName;
                cart.CreateTime = DateTime.Now;
                cart.LastEditUser = userName;
                cart.LastEditTime = DateTime.Now;
                int id = cartDal.InsertT<Cart>(cart);
                if (id > 0)
                {
                    msg.code = 0;
                    msg.message = "成功";
                }
                else
                {
                    msg.code = -1;
                    msg.message = "加入購物車失敗";
                }
            }
            else
            {
                msg.code = -1;
                msg.message = "商品不存在";
            }
            return Redirect("/Cart/Index");

        }

        /// <summary>
        /// 移除購物車
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult Delete(int Id)
        {
            var cartDal =new CartDal();
            if(cartDal.DeleteById(Id) > 0)
            {
                //成功
            }
            else
            {
                //刪除失敗
            }
            return View();
        }
    }
}

?

5. 視圖層展示

?

在Views/Cart/Index.cshtml購物車視圖中,接收控制器傳遞的參數(shù)。如下所示:

@{
    var totalPrice = 0.0M;
}
<div class="content-wrap">
    <div class="content">
        <!-- shopping-cart-area start -->
        <div class="cart-area ptb-100">
            <form action="/Purchase/BuyWithCart" method="post">
                <div class="container">
                    <div class="row">
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">

                            <div class="table-content table-responsive">
                                <table>
                                    <thead>
                                        <tr>
                                            <th class="product-check">選擇</th>
                                            <th class="product-price">圖片</th>
                                            <th class="product-name">產(chǎn)品名稱</th>
                                            <th class="product-price">價格</th>
                                            <th class="product-price">優(yōu)惠價格</th>
                                            <th class="product-quantity">數(shù)量</th>
                                            <th class="product-subtotal">總計</th>
                                            <th class="product-name">刪除</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @{
                                            var cartList = ViewData["CartList"] as List<Cart>;
                                            var productList = ViewData["ProductList"] as List<Product>;
                                            if (cartList.Count > 0)
                                            {
                                                foreach (var cart in cartList)
                                                {
                                                    var product = productList.FirstOrDefault(r => r.Id == cart.ProductId);
                                                    totalPrice = totalPrice + (product.PreferentialPrice * cart.Quantity);
                                                    <tr>
                                                        <td class="product-check">
                                                            <input type="checkbox" value="@(cart.Id)" name="chkCart" style="width:25px;height:25px;" checked="checked" onchange="javascript:checkCartProduct(this);" />
                                                        </td>
                                                        <td class="product-thumbnail">
                                                            <a href="/Product/Detail/@(product.Id)"><img src="@(product.ImageUrl)" alt="" width="100" height="100"></a>
                                                        </td>
                                                        <td class="product-name">
                                                            <a href="/Product/Detail/@(product.Id)">@product.Name</a>
                                                            <br />
                                                            <span style="font-size:12px; color:lightgray">備注:@(string.IsNullOrEmpty(cart.Remark) ? "無" : cart.Remark)</span>
                                                        </td>
                                                        <td class="product-price"><span class="amount">@(Math.Round(product.Price, 2))</span></td>
                                                        <td class="product-price"><span class="amount">@(Math.Round(product.PreferentialPrice, 2))</span></td>
                                                        <td class="product-quantity">
                                                            <input value="@(cart.Quantity)" type="number">
                                                        </td>
                                                        <td class="product-subtotal">@(Math.Round(product.PreferentialPrice * cart.Quantity, 2))</td>
                                                        <td class="product-remove">
                                                            <a href="/Cart/Delete/@(cart.Id)">
                                                                <i class="fa fa-times"><font style="font-size:14px;">刪除</font></i>
                                                            </a>
                                                        </td>
                                                    </tr>
                                                }
                                            }
                                            else
                                            {
                                                <tr><td colspan="7">購物車暫無商品</td></tr>
                                            }
                                        }
                                    </tbody>
                                </table>
                            </div>

                        </div>
                    </div>
                    <div class="row tax-coupon-div">
                        <div class="col-md-7 col-sm-12 col-xs-12">
                        </div>
                        <div class="col-md-5 col-sm-12 col-xs-12">
                            <div class="cart-total">
                                <ul>
                                    <li class="cart-black">總計<span>@totalPrice</span></li>
                                </ul>
                                <div class="cart-total-btn">
                                    <div class="cart-total-btn1 f-left">
                                    </div>
                                    <div class="cart-total-btn2 f-right">
                                        <input type="submit" value="購買" class="go-btn" onclick="javascript:return checkSubmit();" style="background-color: rgb(255, 80, 0);border-width:0px;margin-top:5px;" />
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <!-- shopping-cart-area end -->
    </div>
    <!-- content end -->
</div>
<!-- content-wrap end -->
<script src="~/js/shop.js"></script>

?

購物車頁面展示

?

運行程序,點擊登錄,在登錄成功后,在右上角個人名稱,點擊下拉菜單,選擇購物車,然后打開購物車頁面,如下所示:

[回饋]ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)(五)

以上就是ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)第五部分內(nèi)容,后續(xù)將繼續(xù)介紹其他模塊,敬請期待。文章來源地址http://www.zghlxwxcb.cn/news/detail-623126.html

到了這里,關(guān)于[回饋]ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)(五)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • [回饋]ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)(三)

    [回饋]ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)(三)

    經(jīng)過一段時間的準備,新的一期【ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)】已經(jīng)開始,在之前的文章中,講解了商城系統(tǒng)的整體功能設(shè)計,頁面布局設(shè)計,環(huán)境搭建,系統(tǒng)配置,及首頁【商品類型,banner條,友情鏈接,降價促銷,新品爆款】等功能的開發(fā),今天繼續(xù)講解 商品列表

    2024年02月15日
    瀏覽(19)
  • [回饋]ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)(二)

    [回饋]ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)(二)

    經(jīng)過一段時間的準備,新的一期【ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)】已經(jīng)開始,在之前的文章中,講解了商城系統(tǒng)的整體功能設(shè)計,頁面布局設(shè)計,環(huán)境搭建,系統(tǒng)配置,及首頁商品類型,banner條,友情鏈接等功能的開發(fā),今天繼續(xù)講解首頁的 降價促銷,新品爆款 等內(nèi)容,

    2024年02月16日
    瀏覽(29)
  • [回饋]ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)(完:內(nèi)附源碼)

    [回饋]ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)(完:內(nèi)附源碼)

    經(jīng)過一段時間的準備,【ASP.NET Core MVC開發(fā)實戰(zhàn)之商城系統(tǒng)】已經(jīng)完成,目前代碼已開發(fā)完成,先將全部內(nèi)容整理分享,如有不足之處,還請指正。 ? ? 本系列文章主要講解了商城系統(tǒng)的整體功能設(shè)計,頁面布局設(shè)計,環(huán)境搭建,系統(tǒng)配置,及首頁【商品類型,banner條,友情

    2024年02月11日
    瀏覽(23)
  • asp.net core 框架搭建2-搭建MVC后臺管理系統(tǒng)

    asp.net core 框架搭建2-搭建MVC后臺管理系統(tǒng)

    作者:xcLeigh 文章地址:https://blog.csdn.net/weixin_43151418/article/details/131458964 asp.net core 框架搭建2-搭建MVC后臺管理系統(tǒng) ,本文章介紹asp.net core框架搭建,然后開發(fā)一個后臺管理系統(tǒng),將一步步帶著大家,實現(xiàn)目標。所有操作過程將展現(xiàn)在本篇文章,下面咋們一起來實現(xiàn)它吧。 使

    2024年02月12日
    瀏覽(20)
  • ASP.NET Core MVC -- 將視圖添加到 ASP.NET Core MVC 應(yīng)用

    ASP.NET Core MVC -- 將視圖添加到 ASP.NET Core MVC 應(yīng)用

    右鍵單擊“視圖”文件夾,然后單擊“添加”“新文件夾”,并將文件夾命名為“HelloWorld”。 右鍵單擊“Views/HelloWorld”文件夾,然后單擊“添加”“新項”。 在“添加新項 - MvcMovie”對話框中: 在右上角的搜索框中,輸入“視圖” 選擇“Razor 視圖 - 空” 保持“名稱”框的

    2024年02月13日
    瀏覽(127)
  • ASP.NET Core MVC -- 入門

    ASP.NET Core MVC -- 入門

    ?帶有 ASP.NET 和 Web 開發(fā)工作負載的Visual Studio Visual Studio Code Visual Studio Code 用于 Visual Studio Code 的 C#(最新版本) .NET 7.0 SDK ?ctrl + F5 (開始執(zhí)行,不調(diào)試) 在代碼工作區(qū)間文件夾路徑下打開終端運行下面的命令 ?通過運行以下命令來信任 HTTPS 開發(fā)證書: 編譯運行

    2024年02月11日
    瀏覽(46)
  • ASP.NET Core 中的 MVC架構(gòu)

    ASP.NET Core 中的 MVC架構(gòu)

    MVC架構(gòu)把 App 按照邏輯分成三層: Controllers,接收 http request,配合 model,通過http response 返回 view,盡量不做別的事 Models, 負責業(yè)務(wù)邏輯,App 的狀態(tài),以及數(shù)據(jù)處理 Views,呈現(xiàn) UI,如果UI 較復雜,應(yīng)該使用View 組件, ViewModel, 或者 view 模板 Controller ASP.NET Core MVC 中的所有 Control

    2024年02月09日
    瀏覽(18)
  • ASP.NET Core MVC -- 控制器

    ASP.NET Core MVC -- 控制器

    默認控制器訪問index 特定訪問路徑 ? 特定路徑訪問,帶參數(shù)

    2024年02月12日
    瀏覽(28)
  • 基于ASP.NET MVC開發(fā)的、開源的個人博客系統(tǒng)

    基于ASP.NET MVC開發(fā)的、開源的個人博客系統(tǒng)

    推薦一個功能豐富、易于使用和擴展的開源博客,可以輕松地創(chuàng)建和管理自己的博客。 基于.Net Framework 4.5開發(fā)的、開源博客系統(tǒng),具有豐富的功能,包括文章發(fā)布、分類、標簽、評論、訂閱、統(tǒng)計等功能,同時也可以根據(jù)需要進行自定義擴展。 提供了豐富的配置選項和API,

    2024年02月14日
    瀏覽(30)
  • 【ASP.NET Core】MVC過濾器:常見用法

    【ASP.NET Core】MVC過濾器:常見用法

    前面老周給大伙伴們演示了過濾器的運行流程,大伙只需要知道下面知識點即可: 1、過濾器分為授權(quán)過濾、資源訪問過濾、操作方法(Action)過濾、結(jié)果過濾、異常過濾、終結(jié)點過濾。上一次咱們沒有說異常過濾和終結(jié)點過濾,不過老周后面會說的。對這些過濾器,你有印

    2024年02月05日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包