c# - 购物车页面在 Asp.Net Core 2.0 Razor 页面中仅显示一项
问题描述
有一个问题当我点击添加到购物车按钮时,只显示一个项目,前一个被删除。我有四个 Boostrap 选项卡,每次点击我都会传递选项卡和 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;
}
}
}
- 页面连接到数据库。
- 所有页面都是动态的。
解决方案
因为对于每个 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();
}
如果您要将它们保存在数据库中
推荐阅读
- lookup - Informatica 查找字符串模式匹配
- ios - 捕捉图像方向问题
- python - 如何设置小部件以链接到 jupyternotebooks 的数组
- javascript - 在 html5 画布中渲染大量元素
- java - Oracle 数据库中 Java 的 TLS 版本
- neo4j - 如何在 Cypher 中合并然后排序两个列表?
- python - Python 和 Pandas 使用数据框创建多个动态 Excel 表
- .net - 正则表达式仅检测单个反斜杠并替换为双斜杠
- ios - UserDefault 未在主视图控制器中更新
- python - CUDA DLL 在 Conda 环境中不起作用