Add product to Cookie in Mvc4.0

Source: Internet
Author: User

/// <Summary>
/// Add Cookie
/// </Summary>
/// <Param
Public static int AddCookies (GoodsCookie gs)
{
String userID = null;
If (HttpContext. Current. Session ["hasLogin"]! = Null)
{
UserID = HttpContext. Current. Session ["hasLogin"]. ToString (); // log on
}
ShoppingMallContext db = new ShoppingMallContext ();
Var cookies = HttpContext. Current. Request. Cookies ["MyShoppingCart"]; // create a Cookie and name it
If (cookies = null)
{
String strCookie = gs. skuID + "-" + gs. count + ",";
Cookies = new HttpCookie ("MyShoppingCart") // object initialization
{
Expires = DateTime. Today. AddDays (1), // expiration time
Value = strCookie // Cookie Value assignment
};

HttpContext. Current. Response. Cookies. Add (cookies); // Add to Cookie

If (userID! = Null) // if the user logs on
{
SkuShoppingCart SC = new SkuShoppingCart (); // initialize the shopping cart class.
SC. GoodsSkuID = gs. skuID; // assign a value to the attributes of the class.
SC. ShoppingCartID = db. ShoppingCarts // find the corresponding shopping cart ID based on the user ID
. Where (f => f. UserID = userID)
. Select (f => f. ShoppingCartID)
. Single ();
SC. SkuCount = gs. Count; // Count
Db. SkuShoppingCarts. Add (SC); // Add to database
}
Return 1;
}
Else
{

If (String. IsNullOrWhiteSpace (cookies. Value) // judge whether the Value in Cookies is not empty
{
String strCookie = gs. skuID + "-" + gs. count + ",";
Cookies = new HttpCookie ("MyShoppingCart") // object initialization
{
Expires = DateTime. Today. AddDays (1 ),
Value = strCookie
};
HttpContext. Current. Response. Cookies. Add (cookies); // Add to Cookie
If (userID! = Null)
{
SkuShoppingCart SC = new SkuShoppingCart ();
SC. GoodsSkuID = gs. skuID; // assign a value to the attributes of the class.
SC. ShoppingCartID = db. ShoppingCarts // find the corresponding shopping cart ID based on the user ID
. Where (f => f. UserID = userID)
. Select (f => f. ShoppingCartID)
. Single ();
SC. SkuCount = gs. Count; // Count
Db. SkuShoppingCarts. Add (SC); // Add to database
}
Return 1;
}
Else // return
{
# Region ----- add to dictionary -----

Dictionary <int, int> dict = new Dictionary <int, int> (); // initialize the Dictionary
String goods = cookies. Value; // declare a string variable and assign a Value to Cookie
Goods = goods. Substring (0, goods. Length-1); // specify the string Length
String [] skuidArray = goods. Split (','); // disassemble the string and assign it to the skuidArray
Int count = 0; // declare an Int variable
Foreach (var I in skuidArray) // iteration Array
{
If (I = "" | I = null) // if no value exists
{
Continue; // continue
}
Else // if any
{
String [] strCont = I. Split ('-'); // disassemble the string
Int key = int. Parse (strCont [0]); // The first
Int value = int. Parse (strCont [1]); // The second
// Check whether the key exists in the dictionary.
If (dict. ContainsKey (key ))
{
Dict [key] + = value; // overlay if any
}
Else // No
{
Dict. Add (key, value); // Add to Dictionary
}
}

}
# Endregion

# Region ------- dictionary judgment -------

If (dict. ContainsKey (gs. skuID) // if the dictionary contains this ID
{
Dict [gs. skuID] + = gs. count;
Count + = skuidArray. Count (); // Add the number of them.
}
Else
{
Dict. Add (gs. skuID, gs. count );
Count = skuidArray. Count () + 1; // if not, add a new
}

String countStr = "";
Foreach (var v in dict) // re-iterate the dictionary to spell the string
{
CountStr + = v. Key + "-" + v. Value + ","; // Key-Value Pair
}
Cookies = new HttpCookie (countStr)
{
Expires = DateTime. Today. AddDays (1 ),
Value = countStr
}; // Added to the Cookie

HttpContext. Current. Request. Cookies. Add (cookies );
If (userID! = Null) // if the user logs on
{
SkuShoppingCart SC = new SkuShoppingCart (); // initialize the shopping cart class.
SC. GoodsSkuID = gs. skuID; // assign a value to the attributes of the class.
SC. ShoppingCartID = db. ShoppingCarts // find the corresponding shopping cart ID based on the user ID
. Where (f => f. UserID = userID)
. Select (f => f. ShoppingCartID)
. Single ();
SC. SkuCount = gs. Count; // Count
Db. SkuShoppingCarts. Add (SC); // Add to database
}
Return count; // quantity

# Endregion

}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.