我有呼叫 AJAX 的按鈕
<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) {
alert(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;
var intData = await _context.Inventories.FindAsync(inventoryId);
int availQty = intData.QuantityAvailable;
if ((int)orderedQuantity > curentOrdQuantity)
{
if (availQty < ((int)orderedQuantity - curentOrdQuantity))
{
inventoryOrder.OrderQuantity = curentOrdQuantity;
_context.Update(inventoryOrder);
await _context.SaveChangesAsync();
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();
return Json("");
}
}
return Json("");
}
我沒有在控制器中看到任何錯誤(控制器中的所有更新都在沒有任何問題的情況下發生)但是成功/錯誤功能沒有被執行。我沒有看到任何 alert() 成功或錯誤觸發。我試圖從 JsonResult 更改為 ActionResult ,即使這不起作用。誰能說我在這里想念的是什么。
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熱心網友回復:
當您回傳 return 時Json("");,您可以擺脫 success: function (result)并替換為 success: function (). 現在它會開火alert,你URL redirection會作業。
$.ajax({
type: 'POST',
url: '@Url.Action("EditItem", "Orders")',
data: data,
dataType: "json",
success: function () {
alert("Updated Called");
var url = '@Url.Action("Index", "Orders")';
window.location.href = url "[email protected]";
},
error: function (error) {
alert(error);
}
});
輸出:

注意: 但如果您想繼續檢查結果,那么您可以按照以下步驟操作:
$.ajax({
type: 'POST',
url: '@Url.Action("EditItem", "UserLog")',
data: data,
dataType: "json",
success: function (result) {
console.log(result);
if (result.status === "NotAvailable") {
alert("NotAvailable");
}
else {
var url = '@Url.Action("Index", "Orders")';
window.location.href = url "[email protected]";
}
},
error: function (error) {
alert(error);
}
});
輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/346042.html
標籤:C# 查询 阿贾克斯 asp.net-mvc asp.net核心
