I am trying to display recently viewed products, and so far I have already done so. I have a Product table that stores many products. I have a HomeController that has an Action Details() method that displays product details.
I wrote the AddRecentProduct method, which stores recently viewed products (10) in Session
Now I want to save this recently viewed product list to cookies for at least 30 days on the visitors computer as the session expires. Just like the recently viewed Imdb.
Also, if I create another table in my RecentlyViewed database with rv_id, userId, productId , how can I save RecentViewedList data in this? The userId column will store the loggedIn user ID, but what if the user is a guest (not registered), what is the solution? Do I need to use a GUID?
RecentProduct.cs
public class RecentProduct { public int ProductId { get; set; } public string ProdutName { get; set; } public string ImageUrl { get; set; } public DateTime LastVisited { get; set; } }
controller
public void AddRecentProduct(List<RecentProduct> list, int id, string name, int maxItems) { var item = recentProductList.FirstOrDefault(t => t.ProductId == id); if (item == null) { list.Add(new RecentProduct { ProductId = id, ProdutName = name, LastVisited = DateTime.Now, }); } while (list.Count > maxItems) { list.RemoveAt(0); } } public ActionResult Details(int? id) { if (id == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); Product product = db.Products.Find(id); if (product == null) return HttpNotFound(); var list = Session["RecentProductList"] as List<RecentProduct>; if (list == null) { list = new List<RecentProduct>(); Session["RecentProductList"] = list; } AddRecentProduct(list, id.Value, product.Name, 10); ViewData["RecentProductList"] = list; return View(product); }
ProductDetails Browse Page
<div class="col-sm-9"> @{ var recentProductList = ViewData["RecentProductList"] as List<Project.Models.RecentProduct>; } @foreach (var recentProduct in recentProductList) { <p>@recentProduct.ProdutName (id: @recentProduct.ProductId) </p> } </div>
I get the desired result with the session. Now I want to do this with cookies.
This is what I am trying to create a cookie:
List<RecentProduct> yourList = new List<RecentProduct>(); RecentProduct rc = new RecentProduct(); rc.ProdutName = product.Name; rc.ProductId = product.ProductId; rc.ImageUrl = product.ImagePath; rc.LastVisited = DateTime.Now; yourList.Add(rc); var yourListString = String.Join(",", yourList); // Create a cookie HttpCookie yourListCookie = new HttpCookie("YourList", yourListString); // The cookie will exist for 7 days yourListCookie.Expires = DateTime.Now.AddDays(7); // Write the Cookie to your Response Response.Cookies.Add(yourListCookie);
and In ProductDetails view page to read these files:
@if (Request.Cookies["YourList"] != null) { // Your cookie exists - grab your value and create your List List<string> yourList = Request.Cookies["YourList"].Value.Split(',').Select(x => Convert.ToString(x)).ToList(); // Use your list here <p>@yourList</p> }
I do not get any result. How to read cookies and values?