Mvc里删除Cooki
/// <summary>
/// 删除Cookie
/// </summary>
/// <param name="skuID">从购物车选择要删除的商品ID</param>
/// <returns>先从Cookie里提取出所有的商品并放进一个字典 Dictionary<int, int> dit = new Dictionary<int, int>();里</returns>
/// 从字典里删除传过来的商品,再重新拼接字符串放到Cookie里,
/// 并把从Cookie拼接好的字符串返回到一个声明的集合(List<ShoppingGoods>)里
public static List<ShoppingGoods> DeletCookie(int skuID)
{ if (skuID != )
{
//先从Cookie里提取出所有的商品并放进一个字典(Dictionary<int, int>)里
HttpCookie cookie = HttpContext.Current.Request.Cookies["MyShoppingCart"];//声明Cookie容器
if (cookie != null)//如果Cookie里面有值
{
Dictionary<int, int> dit = new Dictionary<int, int>();//声明字典
string goodsID = cookie.Value;//解析Cookie值并赋值给变量GoodsID
goodsID = goodsID.Substring(, goodsID.Length - );//检索GoodsID字符串的长度
string[] gdsArray = goodsID.Split(',');//检索GoodsID字符串的长度后拆解
int Count = ;//声明Int数量
foreach (var v in gdsArray)//迭代拆解出来的字符串数组
{
if (v == "" || v == null)//判断字符串Value == null || == “”
{
continue;
}
else//如果不为Null或“”
{
string[] strValue = v.Split('-');//拆解字符串
string key = strValue[];//给拆解后的字符串strValue[0]赋值给 string 变量 Key
int strKey = ;//声明int变量
bool parseOKstrKey = int.TryParse(key, out strKey);//转换 string 变量 key字符串为 Int key string value = strValue[];//给字符串的strValue[1]赋值给 string 变量 value
int strCount = ;//声明int变量
bool parseOKstrCount = int.TryParse(value, out strCount);//转换string value字符串为 Int value if (dit.ContainsKey(strKey))//判断字典
{
dit[strKey] += strCount;
Count = gdsArray.Count();//数量统计
}
else
{
dit.Add(strKey, strCount);
Count = gdsArray.Count() + ;
}
}
}
#region 从字典里删除传过来的商品,再重新拼接字符串放到Cookie里
if (dit.ContainsKey(skuID))
{
dit.Remove(skuID);//删除商品
string strCookie = null;//声明拼接剩下的字符串名
ShoppingMallContext db = new ShoppingMallContext();//数据上下文
List<ShoppingGoods> lsg = new List<ShoppingGoods>();//New一个 ShoppingGoods 的集合
foreach (var vdit in dit)//迭代字典里剩余的字符串
{
strCookie += vdit.Key + "-" + vdit.Value + ",";//给字符串strCookie赋值 ShoppingGoods sg = new ShoppingGoods();//New类
sg.GoodsName = db.GoodsSKUs// 很据传过来的vdit.Key查商品名(GoodsName)
.Where(f => f.GoodsID == skuID)
.Select(f => f.Goods.GoodsName)
.ToString();
sg.Goodssku = db.GoodsSKUs //根据传过来的vdit.Key查Goodssku
.Where(f => f.GoodsID == skuID)
.Single();
sg.Count = strCookie[];//根据传过来的vdit.Value给ShoppingGoods 属性Count 赋值 lsg.Add(sg);
}
//并把从Cookie拼接好的字符串返回到一个声明的List<ShoppingGoods>里
var cookies = new HttpCookie(strCookie);
cookies.Expires = DateTime.Today.AddDays();//设置Cookie的过期时间
cookies.Value = strCookie;//Cookie的值
HttpContext.Current.Request.Cookies.Add(cookies);//添加到Cookie
return lsg;
}
else
{
return null;
}
#endregion//从字典里删除传过来的商品,再重新拼接字符串放到Cookie里
}
else
{
return null;
} }
else
{
return TiQuCookies();
}
}
Mvc里删除Cooki的更多相关文章
- 记住 MVC里用formcollection接收form表单传来的值,表单属性必须有name为健!
记住 MVC里用formcollection接收form表单传来的值,input属性必须有name为健! 调了一晚上!! 写个日志记下!!
- tableView里删除单元格
tableView里删除单元格 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSInde ...
- Spring.Net在ASP.NET Mvc里使用的一个小例子
就贴个小例子,就不注意格式了. 1.下载dll NuGet的下载地址:http://docs.nuget.org/docs/start-here/installing-nuget 在vs的NuGet里 ...
- JAVA 从一个List里删除包含另一个List的数据
/** * 从listA里删除listB里有的数据 * @param listA * @param listB * @return */ public static List<String> ...
- mvc 修改 删除 linq
Models文件夹里面可以Linq,Entity两种映射框架,也允许有ADO的操作,甚至可以ADO代码和映射代码一起操作 控制器当中允许有相同的方法名,类似在做修改时有两个Modify方法,但是MVC ...
- 在ASP.NET MVC里对Web Page网页进行权限控制
我们在ASP.NET MVC开发时,有时候还是得设计ASP.NET的Web Page网页(.aspx和.aspx.cs),来实现一些ASP.NET MVC无法实现的功能,如此篇<Visual S ...
- MVC实现删除数据库记录
本次MVC练习,我们想实现对数据库的记录进行删除.在网页的记录列表上,点击删除,将带到另外一个详细页面,显示详细的信息,让用户对删除的信息进行确认,再点击删除动作.不管怎样,得先在数据库创建一个删除的 ...
- MVC入门——删除页
添加Action DeleteUserInfo using System; using System.Collections.Generic; using System.Linq; using Sys ...
- MVC里模型常用的一些操作
学习也是做买卖,归根到底还是学习成本的问题. 下面把微软集合类型的增删改查稍微罗列一下,大家看看它能带来的便利,和你要学的新东西,还有风险(纯粹的数据操作,不用框架,风险其实不大)相比,是否值得.来决 ...
随机推荐
- Eclipse setting Java code style and codetemplate
1.open the eclipse tool window First click the Window menu,then check the children's menu which name ...
- 【Java】使用Runtime执行其他程序
public class ExecDemo{ public static void main(String[] args) { Runtime r = Runtime.getRuntime(); Pr ...
- Makefile与shell脚本区别
http://blog.chinaunix.net/uid-20672257-id-3345593.html 在Makefile可以调用shell脚本,但是Makefile和shell脚本是不同的.本 ...
- android 滚动条
ScrollView简单应用 activity中经常只是一个LinearLayout,但这样的话,如果activity内容超过一屏,无法滚动查看下面的内容. 这时只需在外面嵌套一个ScrollView ...
- 链接脚本之LMA VMA解释
链接脚本中的LMA和VMA是什么意思.这个问题纠结了一段时间,今天在看<ARM体系结构与编程>时,豁然开朗,写下自己的认识.分享例如以下: LMA:载入地址 位于存储器中的地址 LOAD ...
- 数据结构 B树、B-树、B+树、B*概念
B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: B ...
- JQuery EasyUi 扩展combox验证
随笔记录一下 1.通过select text的值验证 /** * 扩展combox验证,easyui原始只验证select text的值,不支持value验证() */ (function($){ c ...
- OleDbHelper
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.D ...
- codevs1145
题目描述 Description 给定A.B.C三根足够长的细柱,在A柱上放有2n个中间有孔的圆盘,共有n个不同的尺寸,每个尺寸都有两个相同的圆盘,注意这两个圆 ...
- mysql学习(九)sql语句
SQL种类: DDL:数据定义语言 DML:数据操作语言 DQL:数据查询语言 DCL:数据控制语言 DDL: show databases; //查询数据库 create database if n ...