我创建了一个网上商店,试图实现“详细查看”功能,.The产品成功地显示了必要的属性,但当查看另一个产品时,前一个产品没有被删除并累积在存储中。因此,详细的产品评审表单包含了我使用请求添加的所有产品。
这是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>
}发布于 2019-04-03 23:10:43
在ASP.NET MVC中,通常首选使用Session以外的方法来存储数据。Session最适合用于multi-screen wizards,其中来自前一个视图/动作的结果适用于下一个视图/动作。
我会给你“快速”的答案;然后我会在稍后编辑我的答案,以提供“更好”的方法。
看起来您正在向Detail类添加多个产品线,而不删除以前的产品线。
如果它是一个列表,但您不想显示多个列表,为什么要将它放在List中?现在,您可以在不从Session中删除项目的情况下使用AddItem。
要从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,这将会有所帮助。
发布于 2019-04-04 03:27:13
我解决方案添加到DetailController放弃会话:
public Detail GetDetail()
{
Detail detail = (Detail)Session["Detail"];
if (detail == null)
{
detail = new Detail();
Session["Detail"] = detail;
}
else {
Session.Abandon();
}
return detail;
}
}
}然而,如何才能更好地解决这样的决定,避免会议?
https://stackoverflow.com/questions/55480260
复制相似问题