首页 > 解决方案 > 如何避免数据重复请求 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>
}

标签: c#asp.net-mvc

解决方案


在 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似乎只希望显示一个结果的地方,那将有所帮助。


推荐阅读