我試圖有一個按鈕,允許用戶更新他們訂購的商品的數量。當用戶嘗試更新超過可用數量的數量時,我什么都不做,并顯示一條錯誤訊息,指出輸入的數量超過可用數量,不應更新資料庫中的任何內容
風景
<form asp-action="EditItem">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
..............
<!--<input type="text" style="border:none;font-size: smaller" id="@("errorMessage")"" readonly /> -->
<div class="form-group">
<label asp-for="OrderQuantity" class="control-label"></label>
<input asp-for="OrderQuantity" id="txt" class="form-control" />
<span asp-validation-for="OrderQuantity" class="text-danger"></span>
</div>
<input type="hidden" id="orderId" name="orderId" value="@Model.OrderId" />
<input type="hidden" id="inventoryorderId" name="inventoryorderId" value="@Model.InventoryOrderId" />
<input type="hidden" id="inventoryId" name="inventoryId" value="@Model.InventoryId" />
<button id="button">Update</button>
</form>
</div>
</div>
@section Scripts {
<script type="text/javascript">
$("#button").click(function () {
var orderedQuantity = $("#txt").val();
var orderId = $("#orderId").val();
var inventoryorderId = $("#inventoryorderId").val();
var inventoryId = $("#inventoryId").val();
var data = {
orderId: orderId,
inventoryorderId: inventoryorderId,
inventoryId: inventoryId,
orderedQuantity: orderedQuantity
};
$.ajax({
type: 'POST',
url: '@Url.Action("EditItem", "Orders")',
data: data,
dataType: "json",
success: function (result) {
if (result !== "") {
if (result.available == "NotAvailable")
$("#errorMessage").val("Enter a valid Quantity");
}
else if (result == "") {
var url = '@Url.Action("Index", "Orders")';
window.location.href = url "[email protected]";
}
},
error: function (error) {
alert(error);
}
});
});
</script>
}
控制器動作
public async Task<JsonResult> EditItem(int? orderId, int? inventoryorderId, int? inventoryId, int? orderedQuantity)
{
var inventoryOrder = await _context.InventoryOrders
.FirstOrDefaultAsync(io => io.InventoryOrderId == inventoryorderId);
int curentOrdQuantity = inventoryOrder.OrderQuantity;
inventoryOrder.OrderQuantity = (int)orderedQuantity;
_context.SaveChanges();
var intData = await _context.Inventories.FindAsync(inventoryId);
int availQty = intData.QuantityAvailable;
if ((int)orderedQuantity > curentOrdQuantity)
{
if (availQty < ((int)orderedQuantity - curentOrdQuantity))
{
return Json(new { status = "NotAvailable", available = intData.QuantityAvailable });
}
else
{
//Updating the Order
inventoryOrder.OrderQuantity = (int)orderedQuantity;
_context.Update(inventoryOrder);
await _context.SaveChangesAsync();
//Updating Inventory
intData.QuantityAvailable = intData.QuantityAvailable - ((int)orderedQuantity - curentOrdQuantity);
_context.Update(intData);
await _context.SaveChangesAsync();
}
}
else if ((int)orderedQuantity < curentOrdQuantity)
{
//Updating the Order
inventoryOrder.OrderQuantity = (int)orderedQuantity;
_context.Update(inventoryOrder);
await _context.SaveChangesAsync();
//Updating Inventory
intData.QuantityAvailable = intData.QuantityAvailable (curentOrdQuantity - (int)orderedQuantity);
_context.Update(intData);
await _context.SaveChangesAsync();
}
return Json("");
}
有兩個問題
當輸入的數量更多時,我正在嘗試,
return Json(new { status = "NotAvailable", available = intData.QuantityAvailable });但資料庫中的 OrderQuantity 仍然更新為輸入的新值。即使我沒有更新代碼中的訂單,它也已更新為新輸入的值。如何將其恢復為舊值而不更改輸入的內容當數量小于 QuantityAvailable 我正在更新庫存和訂單并回傳
return Json("");我希望導航回索引頁面else if (result == "") { var url = '@Url.Action("Index", "Orders")'; window.location.href = url "[email protected]"; }
但什么也沒有發生,它只是停留在 smae 頁面中。任何人都可以幫助我在這里缺少的東西
編輯 第一個問題已經解決,但我仍然無法在 ajax 上獲得成功()作業我試圖在成功中添加警報但我的警報沒有出現
success: function (result) {
alert(result);
if (result !== "") {
if (result.available == "NotAvailable")
$("#errorMessage").val("Enter a valid Quantity");
}
else if (result == "") {
模型類
public class Order
{
public int CustomerNumber { get; set; }
public int CustomerId { get; set; }
public int OrderId { get; set; }
public int InventoryId { get; set; }
public int InventoryOrderId { get; set; }
public string StrainId { get; set; }
public string StrainName { get; set; }
public string StrainCode { get; set; }
public string Age { get; set; }
public string Sex { get; set; }
public string Genotype { get; set; }
public int QuantityAvailable { get; set; }
public int OrderQuantity { get; set; }
public string RoomNumber { get; set; }
}
uj5u.com熱心網友回復:
首要問題
你總是更新資料庫,你的方法的第一行獲取 InventoryOrder 并更新行,我注釋掉了代碼
public async Task<JsonResult> EditItem(int? orderId, int? inventoryorderId, int? inventoryId, int? orderedQuantity)
{
var inventoryOrder = await _context.InventoryOrders
.FirstOrDefaultAsync(io => io.InventoryOrderId == inventoryorderId); // Get the InventoryOrder
int curentOrdQuantity = inventoryOrder.OrderQuantity; // Save the currentOrdQUantity
inventoryOrder.OrderQuantity = (int)orderedQuantity; // UPDATE the OrderQuantity
_context.SaveChanges(); // <- Persist to DB
第二期
我建議你在console.log(result);后面加上success: function (result) {
考慮
使用 ,
Nullable您可以使用.Value來訪問值 ieorderId.Value而不是(int)orderId。由于您傳遞可為空的值,請考慮在轉換或讀取值之前檢查是否為空
uj5u.com熱心網友回復:
恕我直言,你不需要ajax
看法
@model CustomerInventoryModel
form asp-controller="Orders" asp-action="EditItem" method="post">
...... other use controls
<div class="form-group">
<label asp-for="Inventory.QuantityAvailable" class="control-label" readonly></label>
<input asp-for="Inventory.QuantityAvailable " class="form-control" />
</div>
<div class="form-group">
<label asp-for="Inventory.QuantityReq" class="control-label"></label>
<input asp-for="Inventory.QuantityReq" class="form-control" />
<input value="@Model.Inventory.ErrorMessage" class="form-control text-danger float-right" style="border:none;font-size: smaller" />
</div>
<input type="hidden" asp-for="CustomerId" value="@Model.CustomerId" />
<input type="hidden" asp-for="Inventory.InventoryId" value="@Model.Inventory.InventoryId" />
<div>
<button id="buyNow" type="submit"> Buy now </button>
</div>
</form>
將 ErrorMessage 屬性添加到 Inventory 類
public partial class Inventory
{
public int InventoryId { get; set; }
......
[NotMapped]
public string ErrorMessage{ get; set; }
[NotMapped]
public int? QuantityReq { get; set; }
}
并創建詳細視圖模型
public class CustomerInventoryModel
{
public int CustomerId { get; set; }
public Inventory Inventory { get; set; }
}
行動
public async Task<ActionResult> OrderItem(CustomerInventoryModel model)
{
Customer custData = await _context.Customers.FindAsync(model.CustomerID);
var intData = await _context.Inventories.FindAsync(model.Inventory.InventoryID);
if (model.Inventory.QuantityReq <= intData.QuantityAvailable)
{
... your code
return Redirect("Index", new {id=model.CustomerId} );
}
model.ErrorMessage("not enough")
return View(model);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/347652.html
標籤:C# 查询 阿贾克斯 asp.net核心 asp.net-core-mvc
