【发布时间】:2021-06-02 12:07:24
【问题描述】:
在这里我更新了我的数量。我将如何使用或注入这个更新的数量?
[HttpGet]
public ActionResult Details(int? id)
{
ViewBag.product = _db.Spray.ToList();
if (id == null)
{
return NotFound();
}
var hi = _db.Spray.Include(c => c.ProductTypes).FirstOrDefault(c => c.Id == id);
ProductVm product = new ProductVm
{
Name = hi.Name,
Id = hi.Id,
Image=hi.Image,
Image1=hi.Image1,
Quantity = hi.Quantity,
Price = hi.Price,
};
if (product == null)
{
return NotFound();
}
return View(product);
}
[HttpPost]
[ActionName("Details")]
public async Task <IActionResult> ProductDetails(ProductVm pb)
{
List<Spray> sprays = new List<Spray>();
//if (id == null)
//{
// return NotFound();
//}
//var yes = _db.Spray.Include(c => c.ProductTypes).FirstOrDefault(c => c.Id == id);
ProductVm product = new ProductVm()
{
Name = pb.Name,
Id = pb.Id,
Image = pb.Image,
Image1=pb.Image1,
Quantity = pb.Quantity,
Price = pb.Price,
};
if (product == null)
{
return NotFound();
}
sprays = HttpContext.Session.Get<List<Spray>>("sprays");
if (sprays == null)
{
sprays = new List<Spray>();
}
sprays.Add(product);
HttpContext.Session.Set("sprays", sprays);
return RedirectToAction(nameof(Index));
}
[HttpGet]
public IActionResult Cart()
{
List<Spray> sprays = HttpContext.Session.Get<List<Spray>>("sprays");
if (sprays == null)
{
sprays = new List<Spray>();
}
return View(sprays);
}
@model List<Spray>
@{
ViewData["Title"] = "Cart";
}
<div>
<div class="row">
<div class="col-6">
<table class="table table-bordered">
<thead>
<tr>
<th>Image</th>
<th>Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Quantity Update</th>
<th>Total</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
<img src="~/@item.Image" width="200px" height="150px" />
</td>
<td>@item.Name</td>
<td>@item.Quantity</td>
<td>@item.Price</td>
<td>
<input type="number" asp-for="@item.Quantity" min="0" max="1000" />
</td>
<td>@(item.Price * item.Quantity)</td>
<td>
<a asp-area="Customer" asp-action="Remove" asp-controller="LaptopShow" asp-route-id="@item.Id" class="btn btn-danger">
<i class="fas fa-trash"></i>
</a>
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="col-6">
<div class="text-right">
<h3>Total Amount</h3>
<h3>Grand Total : @Model.Sum(c => c.Price * c.Quantity)</h3>
<a asp-area="Customer" asp-action="Checkout" asp-controller="Order" class="btn btn-info">Process To CheckOut</a>
</div>
</div>
<div>
<a asp-action="Index" asp-controller="SprayShow" class="btn btn-primary">Back To Home</a>
</div>
</div>
</div>
@*<div>
@Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
<a asp-action="Index">Back to List</a>
</div>*@
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script>
$(function () {
$("input[name='item.Quantity']").change(function () {
//get the new quantity
var newquantity = parseInt($(this).val());
//update the original quantity value
$(this).closest("tr").find("td")[2].innerHTML = newquantity;
//get the price
var price = parseFloat($(this).closest("tr").find("td")[3].innerHTML);
//calculate the total
$(this).closest("tr").find("td")[5].innerHTML = newquantity * price;
//calcule the Grand Total
var grandtotal = 0;
$("tbody").find("tr").each(function (index, item) {
var value = parseFloat($(item).find("td")[5].innerHTML);
grandtotal += value;
});
$(".text-right h3:last").html("Grand Total : " + grandtotal.toString());
});
});
</script>
上面的代码,我改变了数量的增加和减少来计算。但是这个更新值会话数据无法选择。为此,我在这里发现了一个问题来处理结帐问题。我找不到更新 cart.cshtml 的实际数量数据。 这里我的输出使用 jquery 更新了数量
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HuddsonBay.Data;
using HuddsonBay.Models;
using HuddsonBay.Utility;
using Microsoft.AspNetCore.Mvc;
namespace HuddsonBay.Areas.Customer.Controllers
{
[Area("Customer")]
public class OrderController : Controller
{
private ApplicationDbContext _db;
public OrderController(ApplicationDbContext db)
{
_db = db;
}
[HttpGet]
public IActionResult Checkout()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Checkout(Order anOrder)
{
//session theke data peck
List<Spray> sprays = HttpContext.Session.Get<List<Spray>>("sprays");
if (sprays != null)
{
for each (var mobile in sprays)
{
OrderDetails orderDetails1 = new OrderDetails();
orderDetails1.ProductId = mobile.Id;
anOrder.OrderDetails.Add(orderDetails1);
}
}
anOrder.OrderNo = GetOrderNo();
_db.Orders.Add(anOrder);
await _db.SaveChangesAsync();
HttpContext.Session.Set("sprays", new List<Spray>());
return RedirectToAction(nameof(Checkout));
}
public String GetOrderNo() //for count order number
{
int rowCount = _db.Orders.ToList().Count() + 1;
return rowCount.ToString("000");
}
}
}
以上视图,项目会话数据列表无法选择我更新的数量。我是如何解决这个问题的。我按照数量的价值顺序面临问题。 我是初学者,请任何人帮助。
【问题讨论】:
-
@Pirom 我建议您尝试改写您的问题。作为读者,我不明白你在问什么,所以我不知道如何提供帮助。
-
您获取下一个订单号的代码错误,它会在并发环境中通过创建重复个订单号而失败。请使用数据库序列可靠地生成唯一的订单号。
-
怎么办?有没有办法解决这个问题?
-
在哪里添加 List
sprays 到 session?没有 session,你也可以使用 ajax 向控制器传递数据。 -
@yiyi 我在控制器中添加列表。如果你能解释一下这段代码的 ajax 和 post 控制器,我真的很高兴。
标签: c# asp.net-core asp.net-core-mvc