【发布时间】:2019-01-12 08:50:36
【问题描述】:
有一个问题当我点击添加到购物车按钮时,只显示一个项目并且前一个被删除。我有四个 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;
}
}
}
- 页面已连接到数据库。
- 所有页面都是动态的。
【问题讨论】:
-
通过代码调试时
Footwere.Count的值是多少? -
你认为
Footwere.ProductID = 0;会做什么? -
OnPost Handler的所有代码 sn-ps 都不起作用。我在Footwere.ProductID = 0;上应用了不同的解决方案,但它仍然不起作用,没有OnPost Handlermethod 的结果相同。 -
如何编写
OnPost Handler方法以获得预期结果。 -
我已经尝试过你的代码,每次你点击
Add to Cart,你都会进入OnGet处理程序,它将你重定向到你的购物车页面。你不会在你之前保存你的选择再单击一次。您能否更具体地了解您的方案?单击Add to Cart按钮后,您需要重定向到购物车视图还是停留在索引视图?在哪里调用 OnPost?
标签: c# asp.net-core-2.0