MVC 簡便購物車

2022-07-18 19:51:16 字數 2221 閱讀 2335

效果圖:

(一)controlles

using

system;

using

system.collections.generic;

using

system.linq;

using

system.web;

using

system.web.mvc;

using

namespace

else

listlist = new fruitbf().select();

return view(list);

}public

actionresult viewcart()//購物車的檢視

list

list = session["

cart

"] as list;//

判斷一共花了多少錢

decimal cost = list.sum(p=>p.count*p.fruitprice).value;//

用水果的數量乘以水果的單價

viewbag.sum =cost;

return

view(list);

}public actionresult removefromcart(string id)//刪除

listlist = session["cart"] as list;

var query = list.where(p=>p.fruitcode == id);

if (query.count() > 0)//判斷一下,如果裡面有數值得話,就減去乙個

else

return redirecttoaction("viewcart");//重新整理一下新操作的頁面

}else

}public actionresult buy(string id)//

購買 list

list = session["

cart

"] as list;

var query = list.where(p=>p.fruitcode ==id);

if (query.count() <= 0

)

else

return redirecttoaction("

index");}}

}(二) models

fruitbf

using

system;

using

system.collections.generic;

using

system.linq;

using

system.web;

namespace}}

orderdetails

using

system;

using

system.collections.generic;

using

system.linq;

using

system.web;

namespace

return"";}}

public

decimal

fruitprice

return0;}}

}}

(三) views

@model list

@你已經購買了 @viewbag.count 個商品

@foreach(fruit data in model )

,null)

}@html.actionlink("檢視購物車","viewcart");

效果圖:

您一共購買了 @viewbag.sum 元的商品

@html.actionlink("提交訂單","submitorder")

(四) 新增乙個js

效果圖:

購物車(註冊 登入 購物 購物車 結帳)

購物車 註冊 登入 購物 購物車 結帳 shopping car dict dict money 0 def input username pwd username input username pwd input pwd return username,pwd def goods get with...

購物車原理

1.cookie n cookie儲存在客戶端,且占用很少的資源,瀏覽器允許存放300個cookie,每個cookie的大小為4kb,足以滿足購物車的要求,同時也減輕了伺服器的負荷 n cookie為瀏覽器所內建,使用方便。即使使用者不小心關閉了瀏覽器視窗,只要在cookie定義的有效期內,購物車中...

購物車動畫

金幣終點的x位置 cgfloat positionx 290.0f 終點x 金幣終點的y位置 cgfloat positiony 500.0f 終點y cgmutablepathref path cgpathcreatemutable 金幣的起始x位置 int fromx 20 arc4random...