C# 创建并读取列表<&燃气轮机;从饼干

C# 创建并读取列表<&燃气轮机;从饼干,c#,asp.net-mvc,asp.net-mvc-4,cookies,C#,Asp.net Mvc,Asp.net Mvc 4,Cookies,我试图展示最近浏览过的产品,到目前为止我已经做到了。我有一个Product表,其中存储了许多产品。我有HomeController,它有一个显示产品详细信息的Details()操作方法 我编写了AddRecentProduct方法,在会话中存储最近查看的产品(10) 现在我想将这些最近查看的产品列表存储到访客计算机上的Cookie中至少30天,因为会话过期。就像最近看到的Imdb一样 另外,如果我在数据库recentlyviewd中创建另一个包含列rv\u id、userId、productId

我试图展示最近浏览过的产品,到目前为止我已经做到了。我有一个
Product
表,其中存储了许多产品。我有
HomeController
,它有一个显示产品详细信息的
Details()
操作方法

我编写了
AddRecentProduct
方法,在
会话中存储最近查看的产品(10)

现在我想将这些最近查看的产品列表存储到访客计算机上的Cookie中至少30天,因为会话过期。就像最近看到的Imdb一样

另外,如果我在数据库
recentlyviewd
中创建另一个包含列
rv\u id、userId、productId
的表,我将如何在该表中保存recentlyViewedList数据?userId列将保存loggedIn用户id,但是如果用户是来宾(未注册),那么解决方案是什么?那么我需要使用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);
    }
public void AddRecentProduct(列表、int-id、字符串名、int-maxItems)
{
var item=recentProductList.FirstOrDefault(t=>t.ProductId==id);
如果(项==null)
{
列表。添加(新的最新产品)
{
ProductId=id,
ProdutName=name,
LastVisited=日期时间。现在,
});
}
while(list.Count>maxItems)
{
移除列表(0);
}
}
公共行动结果详细信息(int?id)
{
if(id==null)
返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest);
Product Product=db.Products.Find(id);
如果(产品==null)
返回HttpNotFound();
var list=会话[“RecentProductList”]作为列表;
if(list==null)
{
列表=新列表();
会话[“RecentProductList”]=列表;
}
AddRecentProduct(列表,id.值,产品.名称,10);
ViewData[“RecentProductList”]=列表;
返回视图(产品);
}
产品详细信息查看页面

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

@{
var recentProductList=ViewData[“recentProductList”]作为列表;
}
@foreach(在recentProductList中的var recentProduct)
{
@recentProduct.ProdutName(id:@recentProduct.ProductId)

}
我在会话中得到了期望的结果,现在我想对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);
List yourList=新列表();
RecentProduct rc=新的RecentProduct();
rc.ProdutName=product.Name;
rc.ProductId=product.ProductId;
rc.ImageUrl=product.ImagePath;
rc.LastVisited=DateTime.Now;
添加(rc);
var yourListString=String.Join(“,”,yourList);
//创建一个cookie
HttpCookie yourListCookie=新的HttpCookie(“YourList”,yourListString);
//cookie将存在7天
yourListCookie.Expires=DateTime.Now.AddDays(7);
//将Cookie写入您的响应
Response.Cookies.Add(yourListCookie);
产品详细信息查看页面中阅读如下cookie:

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

    }
@if(Request.Cookies[“YourList”]!=null)
{
//你的cookie存在-抓住你的价值并创建你的列表
列出yourList=Request.Cookies[“yourList”].Value.Split(',')。选择(x=>Convert.ToString(x)).ToList();
//在这里使用您的列表
@你的名单

}

我没有得到任何结果。如何读取cookie和值?

如果您正在使用cookie,为什么需要最近查看的表?在cookie中存储最新的产品ID将适用于登录用户和匿名用户。

最适合您的解决方案是使用Html5 Web存储。
它允许您在本地浏览器中最多存储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);  

});  

顺便说一下,它没有过期时间,您也不需要guid。

请重新阅读问题。现在我将它存储在
会话中,我想将它存储在Cookies中作为产品列表,我说如果我创建一个表来存储最近查看的产品,那么解决方案是什么。