C# 显示最近查看的项目
我有一个产品表格,里面有很多产品。我的HomeController中有一个Details操作方法,它在Details View页面中返回产品详细信息 假设:我有以下数据:C# 显示最近查看的项目,c#,asp.net,asp.net-mvc,asp.net-mvc-4,cookies,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Cookies,我有一个产品表格,里面有很多产品。我的HomeController中有一个Details操作方法,它在Details View页面中返回产品详细信息 假设:我有以下数据: ProductID |产品名称|价格|类别ID 1 | T恤| 120 |服装 2 |衬衫| 150 |服装 3 |手表| 220 |手表 4 |笔记本电脑| 820 |计算 5 |三星S6 | 520 |手机 6 | Xbox | 320 |游戏 现在,我想知道的是,如果有用户访问T恤,那么我想在“最近查看”的新中添加该T
ProductID |产品名称|价格|类别ID
1 | T恤| 120 |服装
2 |衬衫| 150 |服装
3 |手表| 220 |手表
4 |笔记本电脑| 820 |计算
5 |三星S6 | 520 |手机
6 | Xbox | 320 |游戏
现在,我想知道的是,如果有用户访问T恤,那么我想在“最近查看”的新
中添加该T恤产品。如果他再次访问“Watch”,则在“最近查看”中添加手表产品
另外,我想展示更多基于他最近观看的产品的产品。我的意思是,如果他正在访问Xbox,那么Xbox将被添加到最近查看的
中,并且更多与“游戏”类别相关的产品将显示在最近查看的
下面的新
中
这就是我正在尝试的,我的想法是将productId存储在Cookie和Details View页面中,获取Cookie请求,从Cookie获取productId,找到该产品的类别,并显示该类别中的更多产品
HomeController
public ActionResult Details(int? id)
{
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Product product = db.Products.Find(id);
if (product == null)
return HttpNotFound();
//Creating cookies
HttpCookie _userActivity = new HttpCookie("UserActivity");
_userActivity["ProductID"] = id.ToString();
_userActivity["ProdutName"] = product.Name;
_userActivity["Lastvisited"] = DateTime.Now.ToShortTimeString();
//Adding Expire Time of cookies
_userActivity.Expires = DateTime.Now.AddDays(5);
//Adding cookies to current web response
Response.Cookies.Add(_userActivity);
//Reading cookies
HttpCookie readcookie = Request.Cookies["UserActivity"];
string productID , productName, lastVisited ;
if (readcookie != null)
{
productID = readcookie["ProductID"];
productName = readcookie["ProdutName"];
lastVisited = readcookie["Lastvisited"];
}
return View(product);
}
我在我的详细信息查看页面中写了这个。它显示我最近查看产品的Id和详细信息,但只有一个产品的详细信息来自cookies。不像我想象的那样
<div class="row">
<div class="col-sm-9">
@{
var product = Request.Cookies["UserActivity"].Values;
}
<p>@product</p>
<br />
<h2>@Html.DisplayFor(model => model.Name)</h2>
</div>
@{
var product=Request.Cookies[“UserActivity”].值;
}
@产品
@DisplayFor(model=>model.Name)
首先,您应该制作一些课程来保存您最近的产品信息:
public class RecentProduct
{
public int ProductId { get; set; }
public string ProdutName { get; set; }
public DateTime LastVisited { get; set; }
}
public void AddRecentProduct(List<RecentProduct> list, int id, string name, int maxItems)
{
// list is current list of RecentProduct objects
// Check if item already exists
var item = list.FirstOrDefault(t => t.ProductId == id);
// TODO: here if item is found, you could do some more coding
// to move item to the end of the list, since this is the
// last product referenced.
if (item == null)
{
// Add item only if it does not exist
list.Add(new RecentProduct
{
ProductId = id,
ProdutName = name,
LastVisited = DateTime.Now,
});
}
// Check that recent product list does not exceed maxItems
// (items at the top of the list are removed on FIFO basis;
// first in, first out).
while (list.Count > maxItems)
{
list.RemoveAt(0);
}
}
可能正在加载或登录事件中的某个位置使用用户的持久化的最近产品列表填充会话[“RecentProductList”]
然后,您可以使用此功能根据提供的信息存储最新产品:
public class RecentProduct
{
public int ProductId { get; set; }
public string ProdutName { get; set; }
public DateTime LastVisited { get; set; }
}
public void AddRecentProduct(List<RecentProduct> list, int id, string name, int maxItems)
{
// list is current list of RecentProduct objects
// Check if item already exists
var item = list.FirstOrDefault(t => t.ProductId == id);
// TODO: here if item is found, you could do some more coding
// to move item to the end of the list, since this is the
// last product referenced.
if (item == null)
{
// Add item only if it does not exist
list.Add(new RecentProduct
{
ProductId = id,
ProdutName = name,
LastVisited = DateTime.Now,
});
}
// Check that recent product list does not exceed maxItems
// (items at the top of the list are removed on FIFO basis;
// first in, first out).
while (list.Count > maxItems)
{
list.RemoveAt(0);
}
}
public void AddRecentProduct(列表、int-id、字符串名、int-maxItems)
{
//列表是最近产品对象的当前列表
//检查项目是否已存在
var item=list.FirstOrDefault(t=>t.ProductId==id);
//TODO:若在这里找到了该项,您可以进行更多的编码
//将项目移动到列表的末尾,因为这是
//最后引用的产品。
如果(项==null)
{
//仅当项目不存在时添加项目
列表。添加(新的最新产品)
{
ProductId=id,
ProdutName=name,
LastVisited=日期时间。现在,
});
}
//检查最近的产品列表是否不超过maxItems
//(列表顶部的项目根据先进先出原则删除;
//先进先出)。
while(list.Count>maxItems)
{
移除列表(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();
// Read list of recent products from session
var list = Session["RecentProductList"] as List<RecentProduct>;
if (list == null)
{
// If list not found in session, create list and store it in a session
list = new List<RecentProduct>();
Session["RecentProductList"] = list;
}
// Add product to recent list (make list contain max 10 items; change if you like)
AddRecentProduct(list, id.Value, product.Name, 10);
// Store recentProductList to ViewData keyed as "RecentProductList" to use it in a view
ViewData["RecentProductList"] = list;
return View(product);
}
public ActionResult详细信息(int?id)
{
if(id==null)
返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest);
Product Product=db.Products.Find(id);
如果(产品==null)
返回HttpNotFound();
//阅读会话中最近的产品列表
var list=会话[“RecentProductList”]作为列表;
if(list==null)
{
//如果在会话中找不到列表,请创建列表并将其存储在会话中
列表=新列表();
会话[“RecentProductList”]=列表;
}
//将产品添加到最近的列表(制作列表最多包含10项;如果愿意,请更改)
AddRecentProduct(列表,id.值,产品.名称,10);
//将recentProductList存储到视图中键入为“recentProductList”以在视图中使用的数据
ViewData[“RecentProductList”]=列表;
返回视图(产品);
}
然后在视图中,从ViewData dictionary读取数据并显示最新产品的列表:
<div class="row">
<div class="col-sm-9">
@{
var recentProductList = ViewData["RecentProductList"] as List<RecentProduct>;
}
@* Do whatever you like with recentProductList here (e.g. display its data) *@
@foreach (var recentProduct in recentProductList)
{
<p>@recentProduct.ProdutName (id: @recentProduct.ProductId)</p>
}
<br />
<h2>@Html.DisplayFor(model => model.Name)</h2>
</div>
</div>
@{
var recentProductList=ViewData[“recentProductList”]作为列表;
}
@*在此处使用recentProductList执行任何您喜欢的操作(例如,显示其数据)*@
@foreach(在recentProductList中的var recentProduct)
{
@recentProduct.ProdutName(id:@recentProduct.ProductId)
}
@DisplayFor(model=>model.Name)
当然,此代码示例缺少一些空检查和其他内容,但它应该能让您大致了解如何实现此功能。看看您的cookie。您已将其结构化,以便它仅存储一个ProductId的信息。如果你想存储多个产品的信息,你需要存储某种集合,而不是单个Id。@mason我不明白你的意思。对不起。你能举一个你想说什么的例子吗?看看你在饼干里储存了什么。您正在存储单个ProductId。如果您仅将其结构化为存储单个ID,那么您希望它如何显示所有最近访问的产品?每次您使用相同的名称编写cookie时,您都会覆盖已经存在的cookie(因此您只有最后一个productid)。这就是梅森想说的(希望我是对的)。您应该将一些ProductID列表存储到cookie中,或者最好使用其他机制来提供要查看的最新ProductID列表(可能通过ViewData)。@Jure Yeah您是对的。我做错了。如何使用ViewData执行此操作?你能举个例子吗?我喜欢在数据库中存储最新产品的想法,但我想这可能只适用于注册用户,我该如何管理访客?对于访客用户,你还可以将用户信息写入数据库(有限,可能只是某个名称),这样你就可以获得一些用户标识符(ID)。然后将此ID存储在cookie中,并基于cookie中的值,您将获得此来宾用户的最新产品列表。例如,用户标识符可以是GUID。我只获取当前查看的产品的详细信息