c# - 如何避免数据重复请求 MVC
问题描述
我创建了一个在线商店,试图实现“详细视图”功能。该产品已成功显示并具有必要的属性,但是在查看另一个产品时,前一个没有被删除并在存储中累积。因此,详细的产品评论表包含我使用请求添加的所有产品。
这是课Detail
namespace App.Domain.Entities
{
public class Detail
{
private List<DetailLine> lineCollection = new List<DetailLine>();
public void AddItem(Product product)
{
DetailLine line = lineCollection
.Where(g => g.Product.ProductId == product.ProductId)
.FirstOrDefault();
if (line == null)
{
lineCollection.Add(new DetailLine
{
Product = product
});
}
}
public IEnumerable<DetailLine> Lines
{
get { return lineCollection; }
}
}
public class DetailLine
{
public Product Product { get; set; }
}
}
这是DetailController
namespace App.Web.Controllers
{
public class DetailController : Controller
{
public ViewResult Index(string returnUrl)
{
return View(new DetailIndexViewModel
{
Detail = GetDetail(),
ReturnUrl = returnUrl
});
}
private IProductRepository repository;
public DetailController(IProductRepository repo)
{
repository = repo;
}
public RedirectToRouteResult AddToDetail(int productId, string returnUrl)
{
Product product = repository.Products
.FirstOrDefault(g => g.ProductId == productId);
if (product != null )
{
GetDetail().AddItem(product);
}
return RedirectToAction("Index", new { returnUrl });
}
public Detail GetDetail()
{
Detail detail = (Detail)Session["Detail"];
if (detail == null)
{
detail = new Detail();
Session["Detail"] = detail;
}
return detail;
}
}
}
这是期望的结果:
这是麻烦的结果:
此索引视图
@model App.Web.Models.DetailIndexViewModel
@{
ViewBag.Title = "Подробное описание товара";
}
<table class="table">
<thead>
<tr>
<th>Название</th>
<th class="text-left">Цена</th>
<th class="text-left">Вес</th>
<th class="text-left">Вкус</th>
</tr>
</thead>
<tbody>
@foreach (var line in Model.Detail.Lines)
{
<tr>
<td class="text-left">@line.Product.Name</td>
<td class="text-left">@line.Product.Price.ToString("# руб")</td>
<td class="text-left">@line.Product.Mass</td>
<td class="text-left">@line.Product.Taste</td>
</tr>
}
</tbody>
</table>
<tr>
<th class="text-center" >Описание</th>
</tr>
@foreach (var line in Model.Detail.Lines)
{
<div class="container">
@line.Product.Description
</div>
}
解决方案
在 ASP.NET MVC 中,通常首选使用Session
存储数据以外的方法。Session
最适合用于多屏向导,其中上一个视图/动作的结果适用于下一个视图/动作。
我会给你“快速”的答案;然后稍后将编辑我的答案以提供“更好”的方法。
看起来您正在向Detail
课程中添加多个产品线,而没有删除以前的产品线。
如果它是一个列表,但您不想显示多个,为什么要将它放在 a 中List
?现在您AddItem
无需从Session
.
从Session
;中删除一些东西 您需要在适当的位置添加它:
var detail = (Detail)Session["Detail"];
detail.RemoveOlderItems(product);
Session["Detail"] = detail;
看起来RemoveOlderItems
像这样:
public void RemoveOlderItems(Product product)
{
List<DetailLine> lines = lineCollection
.RemoveAll(g => g.Product.ProductId != product.ProductId);
lineCollection = lines;
}
很难说出您要显示的内容(我认为部分原因是我很难按照视图的屏幕截图显示的内容,因为它不是我的母语;但我尽可能地关注我可以)。但我有一些进一步澄清的评论和问题:
视图应该处于什么抽象级别?是一个Detail
充满ProductLines
?您想展示多个产品线吗?根据您显示的屏幕截图,您真的只想显示一个。如果您只需要一个,那么我会完全删除这个想法并在细节上List
拥有一个。ProductLine
如果您可以进一步详细说明您想要的最终结果是什么,以及为什么您有一个List
似乎只希望显示一个结果的地方,那将有所帮助。
推荐阅读
- indexing - sf 的空间索引函数(st_intersects 等)的最佳参数顺序是什么?
- reactjs - 在同一台服务器中使用 HTTPS 连接 react 和 express(centOS7)
- javascript - 时间复杂度 - 图
- python - 小时和时间转换为某种格式
- unit-testing - 有没有办法在 Quasar 中使用全局笑话模拟?
- node.js - 节点流:如何转换 Buffer 流以将数组写入输出
- flutter - 共享首选项中的颤振功能
- python - Python:我有一个列表,我想将它添加到列表的每个元素中
- sql - 行存储功能上的 Sql Server 2019 批处理模式不起作用
- scala - 将 Spark DataFrame 导出到 S3