Hello.
I need to add a new cookie everytime a user enters a page with details about a product and in that page or any similar page i need to display the most recent cookies (Products).
So what i need is: For example i have 4 cookies with name = the name of the product and value = the id of the product.
I need to get all 4 of the cookies compare their value or name and display their properties in a small div in html.
I work in C# in Visual Studio and i am new in this.
Thank you in advance.
What I have tried:
This is my code for setting and getting a cookie Recent.cs :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
public class Recent
{
public static void AddCookie(string name,string nameid)
{
HttpCookie cookie = new HttpCookie(name);
HttpContext.Current.Response.Cookies[name].Value = nameid;
cookie.Value = HttpContext.Current.Response.Cookies[name].Value;
cookie.Expires = DateTime.Now.AddDays(1);
HttpContext.Current.Response.Cookies.Add(cookie);
}
public static string GetCookie(string name)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
if (cookie != null)
{
string objCartListString = cookie.Value.ToString();
string[] objCartListStringSplit = objCartListString.Split('|');
foreach (string s in objCartListStringSplit)
{
string[] ss = s.Split(',');
if (ss[0] != "")
{
Product model = new Product()
{
ProductID = Convert.ToInt32(ss[0]),
};
}
}
}
return name;
}
}
This is my code in ProductDetails.cshtml (First i set the name and the value and then i retreive the cookie with that name):
Recent.AddCookie(@Prod.Btitle.toGreeklish().ToString(),@Prod.ProductID.ToString());
var co = Recent.GetCookie(@Prod.Btitle.toGreeklish().ToString());
This is the html ul where i need to display some of the details of the product (The Foreach loop does not go through all the cookie but instead it goes through the string itself, and for example if the strinng has 7 letters i get 7 instances of the li tag displayed below.):
@if(co != "" && co != null)
{
foreach (var cookie in co)
{
<li>
<div class="single-product">
<div class="product-img">
<a href="#">
<img id="img_c" class="primary-image" src="@prodImg" alt="" />
<img class="secondary-image" src="/img/product/16.jpg" alt="" />
</a>
</div>
<div class="product-content">
<div class="pro-info">
<h2 class="product-name" id="name_c"><a href="#">@co</a></h2>
<div class="price-box">
<span class="new-price" id="price_c">€@Prod.UnitCost.ToString("0.##")</span>
<span class="old-price">£120.00</span>
</div>
</div>
</div>
</div>
</li>
}
}