避免使用foreach asp.net mvc 5进行重复

问题描述

我创建了一个标签,其中添加了他们的产品,然后自动将其添加标签中。但是现在我的问题是,每当我上传相同的数据库时,它都会在选项卡视图“标题”上重复相同的内容。我希望它对它进行分类而不是重复。下面是我创建的代码

下图显示数据库

enter image description here

控制器

    public ActionResult Index()
    {
        return View(db.Products.ToList());
    }

查看

    @model IEnumerable<tkkkk.Models.Product>
    @{
       ViewBag.Title = "Home Page";

       int counterHeader = 0;
       int counterBody = 0;
     }
    <br />
    <ul class="nav nav-tabs">
    @foreach (var product in Model)
    {
        
        if (counterHeader == 0)
        {
            <li class="active"><a data-toggle="tab" href="#@product.ProductId">@product.ProductName</a></li>
        }

        else
        {
            <li><a data-toggle="tab" href="#@product.ProductId">@product.ProductName</a></li>
        }
        counterHeader++;
    }
   </ul>
   <br />

   <div class="tab-content">
   @foreach (var item in Model)
   {
    if (counterBody == 0)
    {
        <div id="@item.ProductId" class="tab-pane fade in active">
            <div class="col-lg-4 col-md-6 mb-4" style="width:200px;">
                <div class="card h-100">
                    <a href="#"><img class="card-img-top" src="http://placehold.it/700x400" alt="" width="200"></a>
                    <div class="card-body">
                        <h4 class="card-title">
                            <a href="#">@item.ProductItem - @item.ProductName</a>
                        </h4>
                        <h5>R @item.ProductPrice</h5>
                    </div>

                </div>
            </div>
        </div>
    }

    else
    {
        <div id="@item.ProductId" class="tab-pane fade">
            <div class="col-lg-4 col-md-6 mb-4" style="width:200px;">
                <div class="card h-100">
                    <a href="#"><img class="card-img-top" src="http://placehold.it/700x400" alt="" width="200"></a>
                    <div class="card-body">
                        <h4 class="card-title">
                            <a href="#">@item.ProductItem - @item.ProductName</a>
                        </h4>
                        <h5>R @item.ProductPrice</h5>
                    </div>

                </div>
            </div>
            </div>
            }
            counterBody++;
            }
        </div>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)