购物车页面在Asp.Net Core 2.0剃须刀页面中仅显示一项

时间:2019-01-12 08:50:36

标签: c# asp.net-core-2.0

有一个问题,当我单击“添加到购物车”按钮时,仅显示一个项目,而上一个项目被删除。我有四个Boostrap选项卡,每次单击时我都会通过tab&id。如何解决此问题,我认为它非常很简单,但是我是Asp.net Core的新手。任何帮助,我们将不胜感激。以下是有关我的应用程序的更多详细信息。

这是我的Index.cshtml页面

<!--remaing three tabs here same as following-->
<div class="tab4">
                    @foreach (var item in Model.FootwereList)
                    {
                        <div class="col-md-3 product-men">
                            <div class="men-pro-item simpleCart_shelfItem">
                                <div class="men-thumb-item">
                                    <img src="@Html.DisplayFor(modelItem => item.ImageaFront)" alt="" class="pro-image-front">
                                    <img src="@Html.DisplayFor(modelItem => item.ImageBack)" alt="" class="pro-image-back">
                                    <div class="men-cart-pro">
                                        <div class="inner-men-cart-pro">
                                            <a asp-page="/single" asp-route-tab="@item.tab" asp-route-id="@item.ProductID" class="link-product-add-cart">Quick View</a>
                                        </div>
                                    </div>
                                    <span class="product-new-top">New</span>

                                </div>
                                <div class="item-info-product ">
                                    <h4><a href="single.html">@Html.DisplayFor(modelItem => item.ProductName)</a></h4>
                                    <div class="info-product-price">
                                        <span class="item_price">$@Html.DisplayFor(modelItem => item.OriginalPrice)</span>
                                        <del>$@Html.DisplayFor(modelItem => item.FalsePrice)</del>
                                    </div>
                                    <div class="snipcart-details top_brand_home_details item_add single-item hvr-outline-out button2">
                                        <form action="/shopping_Cart" method="post">
                                            
                                            </fieldset>
                                        </form>
                                    </div>
                                    <div class="inner-men-cart-pro">
                                        <a asp-page="/shopping_Cart" asp-route-tab="@item.tab" asp-route-id="@item.ProductID" class="link-product-add-cart">Add to cart</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    }
                </div>

这是我的shopping_cart.cshtml页面

@page
@model EliteShopping.Pages.Shopping.shopping_CartModel
@{
}
<h2 style="position:center;">Your Shopping Cart</h2>

    @*<p>
        <a asp-page="Customer">Create New Customer</a>
    </p>*@
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayName("Image")
                </th>
                <th>
                    @Html.DisplayName("Name")
                </th>
                <th>
                    @Html.DisplayName("Price")
                </th>
              
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.FootwereList)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.ImageaFront)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ProductName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.OriginalPrice)
                    </td>
                   
                    <td>
                        <a asp-page="./EditCustomer" asp-route-id="@item.ProductID">Edit Cart Item</a> |
                        <a asp-page="./AllCustomer" onclick="return confirm('Are you sure you want to delete this item?');" asp-page-handler="Delete" asp-route-id="@item.ProductID">Delete Item</a>
                    </td>
                </tr>
            }
        </tbody>
    </table>

这是shopping_cart.cs页面

using EliteShopping.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RazorPagesDemo.Models;
using System.Collections.Generic;
using System.Linq;

namespace EliteShopping.Pages.Shopping
{

    public class shopping_CartModel : PageModel
    {
        DatabaseContext _Context;
        public shopping_CartModel(DatabaseContext databasecontext)
        {
            _Context = databasecontext;
        }
        
        public List<Footwere> FootwereList { get; set; }

        [BindProperty]
        public Footwere Footwere { get; set; }


        public void OnGet(int id, string tab)

        {     //for Footwere
            var Footwere = (from FootwereList in _Context.FootwereTB
                            where FootwereList.ProductID == id & FootwereList.tab == tab
                            select FootwereList).ToList();

            FootwereList = Footwere;
        }

        
      
    }
 }

  • 页面已连接到数据库。
  • 所有页面都是动态的。

1 个答案:

答案 0 :(得分:0)

由于每个Get方法都分配了新的footwereList。

FootwereList = Footwere;

(也正在为实体分配列表) 这就是为什么您总是在购物车中放一个。应该是

FootwereList.Add(Footwere);

我还认为您应该使用数据库或会话来保存项目。就像

_Context.SaveChanges();

我的意思是

    public void OnGet(int id, string tab)

            {     //for Footwere
               var Footwere = _Context.Footwere.Where(p=>p.ProductID==id).FirstOrDefault() ;
               FootwereList.Add(Footwere);
              _Context.SaveChanges();
            }

如果您要将它们保留在数据库中