我正在構建我的第一個 .NET Core MVC 應用程式并使用物體框架。一個按鈕呼叫 AddtoOrder 頁面,它接收來自上一頁的一組引數,AddtoOrder 頁面是允許用戶輸入他們想要訂購的數量的地方。模型類如下
public class Item
{
public int CustomId { get; set; }
public Inventory Inventory { get; set; }
}
Inventory 是另一個類
public partial class Inventory
{
public string Name { get; set; }
public int QuantityAvailable { get; set; }
public string RoomNumber { get; set; }
public int InventoryId { get; set; }
[NotMapped]
public int? QuantityReq { get; set; }
}
此處的 AddtoOrder 視圖QuantityReq是用戶將輸入其他所有從資料庫中檢索到的唯一欄位。
@model JAXSurplusMouseApp.Models.Item
@{
ViewData["Title"] = "Edit";
}
<h4>Add to Order</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="AddtoOrder">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="@Model.Inventory.Name" class="control-label"></label>
<input asp-for="@Model.Inventory.Name" class="form-control" readonly />
</div>
<div class="form-group">
<label asp-for="@Model.Inventory.QuantityAvailable" class="control-label"></label>
<input asp-for="@Model.Inventory.QuantityAvailable" class="form-control" readonly />
</div>
<div class="form-group">
<label asp-for="@Model.Inventory.RoomNumber" class="control-label"></label>
<input asp-for="@Model.Inventory.RoomNumber" class="form-control" readonly />
</div>
</form>
<form method="post"
asp-controller="Inventories"
asp-action="OrderItem">
<label class="control-label">Quantity Required</label>
<input type="text" id="quantityReq" name="quantityReq" value=@Model.Inventory.QuantityReq />
@Html.ValidationSummary(false, "", new { @class = "error" })
<input type="hidden" id="customerID" name="customerID" value="@Model.CustomId" />
<input type="hidden" id="invetoryID" name="invetoryID" value="@Model.Inventory.InventoryId" />
<button type="submit"><u>Order</u></button>
</form>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
所以我需要根據資料庫中的值驗證用戶輸入的值,所以在控制器中,我正在比較如下值,如果用戶輸入的數量大于資料庫else if (quantityReq > intData.QuantityAvailable)中可用的數量OrderItem
// Action to launch the AddtoOrder page
public async Task<IActionResult> AddtoOrder(int? inventoryID, int? custID)
{
if (inventoryID == null || custID == null)
{
return NotFound();
}
Customer custData = await _context.Customers.FindAsync(custID);
var inventories = await _context.Inventories.FindAsync(inventoryID);
var model = new Item
{
CustomId = (int)custID,
Inventory = inventories
};
return View(model);
}
//Action athat allows the users to submit the order
public async Task<IActionResult> OrderItem(int? customerID, int? invetoryID, int quantityReq)
{
if (customerID == null || invetoryID == null)
{
return NotFound();
}
Customer custData = await _context.Customers.FindAsync(customerID);
var intData = await _context.Inventories.FindAsync(invetoryID);
if (quantityReq <= intData.QuantityAvailable && quantityReq > 0)
{
InventoryOrder io = new InventoryOrder();
io.OrderQuantity = quantityReq;
io.InventoryId = (int)invetoryID;
_context.Add(io);
await _context.SaveChangesAsync();
intData.QuantityAvailable = intData.QuantityAvailable - quantityReq;
_context.Update(intData);
await _context.SaveChangesAsync();
return RedirectToAction("Index", "Inventories", new { id = customerID });
}
else if (quantityReq > intData.QuantityAvailable){
ModelState.AddModelError("QuantityReq", "Quantity entered more than Quantity Available");
return RedirectToAction("AddtoOrder", "Inventories", new { inventoryID = invetoryID, custID = customerID });
}
}
由于我從操作回傳到不同的視圖,因此驗證錯誤未顯示在操作的AddtoOrder頁面上OrderItem。我該如何處理驗證錯誤!
uj5u.com熱心網友回復:
您可以使用 TempData 來存盤驗證失敗標志,如下所示:
[HttpGet]
public IActionResult AddtoOrder(int? inventoryID, int? custID)
{
if (inventoryID == null || custID == null)
{
return NotFound();
}
Customer custData = await _context.Customers.FindAsync(custID);
var inventories = await _context.Inventories.FindAsync(inventoryID);
var model = new Item
{
CustomId = (int)custID,
Inventory = inventories
};
//add the following code to judge if validation fail or not...
if(TempData["Error"]!=null)
{
ModelState.AddModelError("Inventory.QuantityReq", "Quantity entered more than Quantity Available");
}
return View(model);
}
[HttpPost]
public async Task<IActionResult> OrderItem(int? customerID, int? invetoryID, int quantityReq)
{
//...
else if (quantityReq > intData.QuantityAvailable)
{
TempData["Error"] = "Validation fail"; //add this...
return RedirectToAction("AddtoOrder", "Inventories", new { inventoryID = invetoryID, custID = customerID });
}
return View();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/347659.html
標籤:javascript 查询 阿贾克斯 asp.net核心 asp.net-ajax
上一篇:在blazor中使用自定義組件時,在注入的差異服務中呼叫HttpContextAccesor后為null
下一篇:在AWSS3上檢查上傳成功
