2015-12-07 40 views
5

我正在嘗試顯示最近查看的產品,至此我已經完成了該操作。我有一個Product表,有很多產品存儲。我有HomeController,其中有一個顯示產品詳細信息的Details()的Action方法。創建並從Cookie中讀取<>

我已經寫了AddRecentProduct方法存儲在Session

最近瀏覽過的產品(10)現在我要存儲這些最近瀏覽的產品列表進入的cookie ATLEAST30天訪客的計算機上,因爲會話過期。就像Imdb最近查看。

另外如果我在我的數據庫RecentlyViewed與列rv_id, userId, productId創建另一個表我將如何保存recentViewedList數據在此? userId列將保存loggedIn用戶的ID,但如果用戶是Guest(未註冊),那麼解決方案是什麼?我需要使用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; } 
} 

控制器

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); 
    } 

產品詳細查看頁面

<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> 

我通過會話獲得期望的結果,現在我想用cookies來做同樣的事情。

這就是我想要創建的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); 

,並在產品詳細查看頁面閱讀餅乾這樣的:

@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> 

    } 

我沒有得到任何結果。我怎樣才能讀取cookie和值?

回答

-1

如果您使用cookies,您爲什麼需要RecentlyViewed表呢?將最近的產品id存儲在cookie中對登錄用戶和匿名用戶都有效。

+0

請再次閱讀該問題。現在我將它存儲在'Session'中,並且我想將它作爲產品列表存儲在Cookies中,並且我說** IF **我創建了一個用於存儲最近查看的產品的表格,這將是解決方案。 – user3223395667

0

最適合您的解決方案是使用Html5網絡存儲。
它可讓您在本地瀏覽器中存儲高達5MB的數據。
你只能通過javascript讀寫。
例如:

$('#btnLocalStorage').click(function() 
{ 
    var txtName = $("#txtName").val(); 
    //set item 
    localStorage.setItem("EmpName", txtName); 
}); 

$('#btnLocalStorageRemove').click(function() 
{ 
    //remove item 
    localStorage.removeItem("EmpName"); 
}); 
$('#btnLocalStorageClear').click(function() 
{ 
    //clear Local Storage 
    localStorage.clear(); 
}); 
$('#btnLocalStorageLength').click(function() 
{ 
    var localStoragelength = localStorage.length; 
    alert("The length of Local storage is " + localStoragelength); 

}); 

通過它沒有過期時間和u不需要GUID的方式。

相關問題