简体   繁体   English

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

[英]Shopping Cart Page display only one item in Asp.Net Core 2.0 Razor page

There is a problem When I clicked on Add to cart Button ,only one item is displayed and previous one is deleted .I have four Boostrap tab,on every click I am passing tab&id .How to fix this problem I think its very simple but I am new in Asp.net Core.Any help it would be great appreciate.Following are more details about my application.有一个问题当我点击添加到购物车按钮时,只显示一个项目,前一个被删除。我有四个 Boostrap 选项卡,每次点击我都会传递 tab&id 。如何解决这个问题我认为这很简单,但我我是 Asp.net Core 的新手。如果有任何帮助,将不胜感激。以下是有关我的应用程序的更多详细信息。

This is my Index.cshtml page这是我的 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>

This is my shopping_cart.cshtml Page这是我的 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>

This is shopping_cart.cs page这是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; } } }

  • Pages are conntected to the database.页面连接到数据库。
  • All pages are dynamic.所有页面都是动态的。

Because with every Get method you assign new footwereList.因为对于每个 Get 方法,您都会分配新的 footwereList。

FootwereList = Footwere;

(Also you are assigning a list to an entity) Thats why you always have one in the cart. (您还为实体分配了一个列表)这就是为什么您的购物车中总是有一个列表。 It should be它应该是

FootwereList.Add(Footwere);

also I think you should use either database or session to keep items.Like我也认为你应该使用数据库或会话来保留项目。喜欢

_Context.SaveChanges();

I mean我的意思是

    public void OnGet(int id, string tab)

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

If you are going to keep them in Database如果您要将它们保存在数据库中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM