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里模型常用的一些操作
学习也是做买卖,归根到底还是学习成本的问题. 下面把微软集合类型的增删改查稍微罗列一下,大家看看它能带来的便利,和你要学的新东西,还有风险(纯粹的数据操作,不用框架,风险其实不大)相比,是否值得.来决 ...
随机推荐
- bzoj2014 [Usaco2010 Feb]Chocolate Buying
Description 贝西和其他奶牛们都喜欢巧克力,所以约翰准备买一些送给她们.奶牛巧克力专卖店里 有N种巧克力,每种巧克力的数量都是无限多的.每头奶牛只喜欢一种巧克力,调查显示, 有Ci头 ...
- OC基础9:预处理程序
"OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1. 关于#define语句: (1). ...
- BZOJ 3181([Coci2012]BROJ-最小质因子为p的第k小素数)
3181: [Coci2012]BROJ Time Limit: 10 Sec Memory Limit: 64 MB Submit: 26 Solved: 7 [ Submit][ Stat ...
- Oracle11g重建EM 报ORA-20001: SYSMAN already exists
日志: Apr , :: PM oracle.sysman.emcp.EMReposConfig createRepository : SYSMAN already exists.. ORA-0651 ...
- javascript的几种时间格式
1.当前系统区域设置格式(toLocaleDateString和toLocaleTimeString) 例子:(new Date()).toLocaleDateString() + " &q ...
- HTTP Status 404 - No result defined for action com.hebky.oa.classEntity.action.EntitysAction and result input
在开发中总遇到这个问题,No result defined for action:原因:Action中的属性值为空的时候,Struts2的默认拦截器会报错,但是又找不到input的Result,不能够 ...
- 【转】这些隐藏在苹果iPhone当中的内置代码你是否知道?
原文地址:http://www.ithome.com/html/iphone/200569.htm 每一部手机都有一些内置的隐藏代码,我们称之为“指令”,这些代码加在一起就叫做“指令集”.当然这些代码 ...
- HTML3层叠样式表
层叠样式表:CSS Cascading Style Sheet.V2.1 控制页面样式外观. 一.样式表分三类: 1.内联样式表.--放在元素的开始标记中.--只对当前元素起作用. <in ...
- eclipse 使用maven 创建web3.1项目
最近刚刚开始学java, 在框架搭建的过程中, 遭遇了诸多的坑, 和各种各样的不解与困惑, 由于没有什么java的相关基础, 看到各种xml的配置文件, 当真是一个头两个大. 并且并不知道那个配置到底 ...
- 如何使用for循环连续的实例化多个对象!
Test类import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Test ...