一.思路邏輯: 首先我先來說一下我自己的理解,一個萌新的見解,要實現購物車的功能,首先要獲取到登錄時的用戶id及商品的編號(商品id),這裡我用的模式是mvc模式進行實現功能的,用戶登錄時,利用session保存用戶的登錄用戶名,然後在控制器里進行傳值操作,定義一個session進行接收用戶輸入的用 ...
一.思路邏輯:
首先我先來說一下我自己的理解,一個萌新的見解,要實現購物車的功能,首先要獲取到登錄時的用戶id及商品的編號(商品id),這裡我用的模式是mvc模式進行實現功能的,用戶登錄時,利用session保存用戶的登錄用戶名,然後在控制器里進行傳值操作,定義一個session進行接收用戶輸入的用戶名,登錄成功後進行保存用戶的用戶名,登錄成功,前臺在進行跳轉到顯示界面,點擊事先創建好的購物車按鈕,把我們已經保存好的用戶名傳過去,在進行session接收用戶名字,添加到購物車時,前面我也說到需要兩個值,我們現在已經獲取到了用戶id(用戶名),再獲取到商品id就可以進行添加到購物車功能的實現,在顯示的ajax拼接字元串進行顯示的時候,我們需要再添加一個多選按鈕(多選按鈕是為了進行多項數據選擇時,添加到購物車以及添加收藏時更方便一些),為多選按鈕添加一個id屬性或者name屬性,這裡是為了我們方便獲取它的數據,獲取多選框的id值的方法我就不在這裡過多介紹了,既然我們需要的兩個值都已經獲取到,我們的添加購物車功能就可以實現了,今天先寫這麼多,明天還要周考,在以後的時間里我會繼續修改和添加這篇文章的後續內容,大佬們看過之後,若是有空閑時間,在評論區多給小學生一些建議,我會進行改正的.今天我就說到這裡了,購物車的添加基本說完了,後續我會及時利用空閑時間進行後續功能及代碼思路邏輯的更新.
二.代碼如下:
實例化模型層(model層),共創建了四個表,我用的方法是EF架構中的codefirst方法,詳細解釋大家可以百度,或者可以看一看另一個博主的博客,https://www.cnblogs.com/zpyplan/p/9565863.html:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.ComponentModel.DataAnnotations; 7 using System.ComponentModel.DataAnnotations.Schema; 8 9 namespace MODEL 10 { 11 //購物車表 12 [Table("MyShoppingCar")] 13 public class MyShoppingCar 14 { 15 [Key] 16 public int Id { get; set; } 17 public string UserId { get; set; } 18 public string Pno { get; set; } 19 public int? Account { get; set; } 20 } 21 }

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.ComponentModel.DataAnnotations; 7 using System.ComponentModel.DataAnnotations.Schema; 8 9 namespace MODEL 10 { 11 //收藏表 12 [Table("MyFavorite")] 13 public class MyFavorite 14 { 15 [Key] 16 public string UserId { get; set; } 17 public string Pno { get; set; } 18 } 19 }

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel.DataAnnotations; 4 using System.ComponentModel.DataAnnotations.Schema; 5 6 namespace MODEL 7 { 8 //商品表 9 [Table("Product")] 10 public class Product 11 { 12 [Key] 13 public int Id { get; set; } 14 public string Pno { get; set; } 15 public string Pname { get; set; } 16 public int? Price { get; set; } 17 public string ImgPath { get; set; } 18 } 19 }

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.ComponentModel.DataAnnotations; 7 using System.ComponentModel.DataAnnotations.Schema; 8 9 namespace MODEL 10 { 11 //登錄用戶表 12 [Table("UserInfo")] 13 public class UserInfo 14 { 15 [Key] 16 public String UserID { get; set; } 17 public String UserName { get; set; } 18 public String WX { get; set; } 19 public String Pwd { get; set; } 20 public String QQ { get; set; } 21 } 22 }
搭建好model層,我們要開始寫dal層里的方法了,我們要實現的功能有用戶登錄功能,商品的顯示功能,添加到購物車功能,加減一功能,收藏功能,顯示購物車列表,批量刪除購物車
dal層如下(codefirst方法):

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace DAL 8 { 9 public class MyFavoriteDAL 10 { 11 public int Favorite(string userid,string pnos) 12 { 13 string[] arr = pnos.Trim(',').Split(','); 14 using (Model1 mc = new Model1()) 15 { 16 foreach (string str in arr) 17 { 18 string sql = $"insert into MyFavorite(userid,pno) values('{userid}','{str}')"; 19 mc.Database.ExecuteSqlCommand(sql); 20 } 21 } 22 23 return 1; 24 } 25 } 26 }

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using MODEL; 7 8 namespace DAL 9 { 10 public class MyShoppingCarDAL 11 { 12 //添加到購物車的方法 13 public int AddMyShoppingCar(string userid, string pnos) 14 { 15 string[] arr = pnos.Trim(',').Split(','); 16 using (Model1 mc = new Model1()) 17 { 18 foreach (string str in arr) 19 { 20 string sql = $"insert into MyShoppingCar(userid,pno,Account) values('{userid}','{str}',1)"; 21 mc.Database.ExecuteSqlCommand(sql); 22 } 23 } 24 25 return 1; 26 } 27 28 //獲取購物車的信息 29 public List<V_MyShoppingCar> GetList(string userid) 30 { 31 using (Model1 mc = new Model1()) 32 { 33 34 var query = from s in mc.Products 35 from t in mc.MyShoppingCars 36 where s.Pno == t.Pno && t.UserId== userid 37 select new V_MyShoppingCar { Pno = s.Pno, Pname = s.Pname, Price = s.Price, Id = t.Id, Account = t.Account, TotalMoney = t.Account * s.Price, ImgPath=s.ImgPath }; 38 return query.ToList(); 39 } 40 } 41 42 //批量刪除 43 public int DelMyShoppingCars(string ids) 44 { 45 //1,2,3,4,.... 46 using (Model1 mc = new Model1()) 47 { 48 string sql = $"delete MyShoppingCar where id in({ids.Trim(',')})"; 49 mc.Database.ExecuteSqlCommand(sql); 50 } 51 return 1; 52 } 53 54 //加減1 55 public int MyShoppingCarsUpDown(string id,string sType) 56 { 57 using (Model1 mc = new Model1()) 58 { 59 string sql; 60 if (sType.Equals("up")) 61 sql = $"update MyShoppingCar set Account=Account+1 where id={id}"; 62 else 63 sql = $"update MyShoppingCar set Account=Account-1 where id={id}"; 64 mc.Database.ExecuteSqlCommand(sql); 65 } 66 return 1; 67 } 68 69 } 70 }

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using MODEL; 7 8 namespace DAL 9 { 10 public class ProductDAL 11 { 12 //商品顯示的方法 13 public List<Product> GetList(string pname) 14 { 15 using (Model1 mc = new Model1()) 16 { 17 //linq查詢 18 return mc.Products.Where(x=>x.Pname.Contains(pname)).ToList(); 19 } 20 } 21 22 23 24 } 25 }

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace DAL 8 { 9 public class UserInfoDAL 10 { 11 //用戶登良路的方法 12 public int Login(string userid,string pwd) 13 { 14 using (Model1 mc = new Model1()) 15 { 16 //linq查詢的方法 17 return mc.UserInfos.Where(x => x.UserID.Equals(userid) && x.Pwd.Equals(pwd)).Count(); 18 } 19 } 20 } 21 }
控制器里的方法(因為這裡我是搭三層寫的,有個bll層,也就是業務邏輯層,控制器里調用的方法大多是調用的業務邏輯層的方法,因為我吧所有業務處理的代碼都寫在了dal層,我在這裡就不寫bll層了,複製代碼時只需將bll層的方法調用替換成dal層的方法調用):

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using BLL; 7 using MODEL; 8 9 namespace WT01.Controllers 10 { 11 public class HomeController : Controller 12 { 13 UserInfoBLL bll = new UserInfoBLL(); 14 15 //登錄頁面 16 public ActionResult Login() 17 { 18 return View(); 19 } 20 //顯示頁面 21 public ActionResult Index() 22 { 23 return View(); 24 } 25 //購物車頁面 26 public ActionResult MyShoppingCar() 27 { 28 return View(); 29 } 30 31 //登錄驗證 32 [HttpPost] 33 public int LoginValidate(string userid,string pwd) 34 { 35 HttpContext.Session["userid"] = userid; 36 return bll.Login(userid, pwd); 37 } 38 39 //收藏 40 [HttpPost] 41 public int Favorite(string pnos) 42 { 43 string userid= HttpContext.Session["userid"].ToString(); 44 return new MyFavoriteBLL().Favorite(userid, pnos); 45 } 46 47 //加入購物車 48 [HttpPost] 49 public int AddMyShoppingCar(string pnos) 50 { 51 string userid = HttpContext.Session["userid"].ToString(); 52 return new MyShoppingCarBLL().AddMyShoppingCar(userid, pnos); 53 } 54 55 56 //獲取產品的List 57 [HttpGet] 58 public JsonResult GetList(string pname) 59 { 60 ProductBLL productBLL = new ProductBLL(); 61 return Json(productBLL.GetList(pname),JsonRequestBehavior.AllowGet); 62 } 63 64 65 //獲取我的購物車列表信息List 66 [HttpGet] 67 public JsonResult GetMyShoppingCarList() 68 { 69 MyShoppingCarBLL myShoppingCar = new MyShoppingCarBLL(); 70 string userid = HttpContext.Session["userid"].ToString(); 71 return Json(myShoppingCar.GetList(userid), JsonRequestBehavior.AllowGet); 72 } 73 74 //批量刪除購物車 75 [HttpPost] 76 public int DelMyShoppingCar(string ids) 77 { 78 return new MyShoppingCarBLL().DelMyShoppingCars(ids); 79 } 80 81 //加減1 82 [HttpPost] 83 public int MyShoppingCarsUpDown(string id, string sType) 84 { 85 return new MyShoppingCarBLL().MyShoppingCarsUpDown(id, sType); 86 } 87 88 } 89 }
登錄視圖中的代碼如下:

1 @{ 2 ViewBag.Title = "Login"; 3 4 } 5 <h2>Login</h2> 6 <script src="~/Scripts/jquery-3.3.1.min.js"></script> 7 <script> 8 9 //驗證登錄 10 function LoginCheck() { 11 var userid = $("#txtAmount").val(); 12 var pwd = $("#txtPwd").val(); 13 14 if (userid == "") { 15 alert("賬號不能為空!"); 16 return; 17 } 18 if (pwd == "") { 19 alert("賬號不能為空!"); 20 return; 21 } 22 23 24 $.ajax({ 25 url: '/Home/LoginValidate', 26 type: 'post', 27 dataType: 'json', 28 data: { userid: userid, pwd: pwd }, 29 success: function (data) { 30 if (data > 0) { 31 location.href = '/Home/Index'; 32 } 33 else { 34 alert("賬號或密碼錯誤,請重新輸入"); 35 location.href = '/Home/Login'; 36 } 37 } 38 }) 39 } 40 41 </script> 42 <table border="1"> 43 <tr> 44 <td>賬號:</td> 45 <td><input type="text" id="txtAmount" /></td> 46 </tr> 47 <tr> 48 <td>密碼:</td> 49 <td><input type="password" id="txtPwd" /></td> 50 </tr> 51 <tr> 52 <td colspan="2"> 53 <input value="登錄" type="button" id="btnLogin" onclick="LoginCheck()" /> 54 </td> 55 </tr> 56 </table>
商品顯示的視圖代碼如下:

1 @{ 2 ViewBag.Title = "Home Page"; 3 Layout = null; 4 } 5 <script src="~/Scripts/jquery-3.3.1.min.js"></script> 6 7 <script> 8 9 //文檔就緒函數 10 $(function () { 11 QueryList(); 12 }) 13 14 //收藏 15 function MyFavorite() { 16 var arr = document.getElementsByName("xselect"); 17 var str = ""; 18 for (var i = 0; i < arr.length; i++) { 19 if (arr[i].checked) 20 str += arr[i].id + ","; 21 } 22 //alert(str); 23 $.ajax({ 24 url: '/Home/Favorite', 25 type: 'post', 26 dataType: 'json', 27 data: { pnos: str }, 28 success: function (data) { 29 if (data > 0) 30 alert("收藏成功!"); 31 } 32 }) 33 } 34 35 //加入購物車 36 function MyShoppingCar() { 37 var arr = document.getElementsByName("xselect"); 38 var str = ""; 39 for (var i = 0; i < arr.length; i++) { 40 if (arr[i].checked)

-
本系列將和大家分享面向對象23種設計模式中常用的幾種設計模式,本章主要簡單介紹下行為型設計模式。 ...
-
都0202年了,不會還有人在用WPF吧,不會吧不會吧~什麼qt啊,electron啊,flutter啊好多東西要學啊,我還是繼續用WPF吧。因為其它的還不太會;繼續學習吧~ 期待WinUI3和“MAUI”-2021 1.周末沒加班,閑來沒事做;模仿個東西好了;在模仿中學習(各位大佬不要嘲諷我就是~) ...
-
首次發表,不太會寫,那點乾貨上個分,廢話不多說,不懂多看 1 public void Publist() 2 { 3 ResultListData resultData = new ResultListData(); 4 BLL.basic_project bllspro = new BLL.bas ...
-
.net core 和.net framework上傳文件還是有一些區別的有很多註意的地方 .net framework 上傳文件用httppostedfilebase .net core 上傳文件用 IFormFile 下麵廢話不多說了,直接上代碼 控制器裡面寫 using System; usi ...
-
一、簡介 ABP vNext 在 v 2.9.x 版本當中添加了 BLOB 系統,主要用於存儲大型二進位文件。ABP 抽象了一套通用的 BLOB 體系,開發人員在存儲或讀取二進位文件時,可以忽略具體實現,直接使用 IBlobContainer 或 IBlobContainer<T> 進行操作。官方的 ...
-
使用 .NET Core WebAPI 實現一個 VPS 下載中轉(加速)器 VPSDownloader.NET,暨 .NET Core 程式部署到 Linux 系統。 ...
-
最近想給我的框架加一種功能,就是比如給一個方法加一個事務的特性Attribute,那這個方法就會啟用事務處理。給一個方法加一個緩存特性,那這個方法就會進行緩存。 這個也是網上說的面向切麵編程AOP。 AOP的概念也很好理解,跟中間件差不多,說白了,就是我可以任意地在方法的前面或後面添加代碼,這很適合 ...
-
Tips:本篇已加入系列文章閱讀目錄,可點擊查看更多相關文章。 前言 上一篇【.Net Core in Docker極簡入門(上篇)】講解了docker的一些基本命令和操作,併成功構建了自己的asp.net core web應用的鏡像,啟動容器。本篇繼續。 開始 上一篇的項目例子非常簡單,通常我們的 ...
-
比如要拆分“呵呵呵90909086676喝喝999”,下麵當type=0返回的是中文字元串“呵呵呵,喝喝”,type=1返回的是數字字元串“90909086676,999”, private string GetStrings(string str,int type=0) { IList<strin ...
-
Swagger一個優秀的Api介面文檔生成工具。Swagger可以可以動態生成Api介面文檔,有效的降低前後端人員關於Api介面的溝通成本,促進項目高效開發。 1、使用NuGet安裝最新的包:Swashbuckle.AspNetCore。 2、編輯項目文件(NetCoreTemplate.Web.c ...
-
2020 年 7 月 30 日, 由.NET基金會和微軟 將舉辦一個線上和為期一天的活動,包括 微軟 .NET 團隊的演講者以及社區的演講者。本次線上大會 專註.NET框架構建微服務,演講者分享構建和部署雲原生應用程式的最佳實踐、模式、提示和技巧。有關更多信息和隨時瞭解情況:https://focu... ...
-
#abp框架Excel導出——基於vue #1.技術棧 ##1.1 前端採用vue,官方提供 UI套件用的是iview ##1.2 後臺是abp——aspnetboilerplate 即abp v1,https://github.com/aspnetboilerplate/aspnetboilerp ...
-
前言 本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯繫我們以作處理。 作者:碧茂大數據 PS:如有需要Python學習資料的小伙伴可以加下方的群去找免費管理員領取 input()輸入 Python提供了 input() 內置函數從標準輸入讀入一 ...
-
從12年到20年,python以肉眼可見的趨勢超過了java,成為了當今It界人人皆知的編程語言。 python為什麼這麼火? 網路編程語言搜索指數 適合初學者 Python具有語法簡單、語句清晰的特點,這就讓初學者在學習階段可以把精力集中在編程對象和思維方法上。 大佬都在用 Google,YouT ...
-
在社會上存在一種普遍的對培訓機構的學生一種歧視的現象,具體表現在,比如:當你去公司面試的時候,一旦你說了你是培訓機構出來的,那麼基本上你就涼了,那麼你瞞著不說,然後又通過了面試成功入職,但是以後一旦在公司被髮現有培訓經歷,可能會面臨被降薪,甚至被辭退,培訓機構出來的學生,在用人單位眼裡就是能力低下的 ...
-
from typing import List# 這道題看了大佬寫的代碼,經過自己的理解寫出來了。# 從最外圍的四周找有沒有為O的,如果有的話就進入深搜函數,然後深搜遍歷# 判斷上下左右的位置是否為Oclass Solution: def solve(self, board: List[List[s ...
-
import requests; import re; import os; # 1.請求網頁 header = { "user-agent":'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, li ...
-
import requests; import re; import os; import parsel; 1.請求網頁 header = { "user-agent":'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537. ...
