我正在構建我的第一個 .NET Core MVC 應用程式并使用物體框架。我有一個編輯頁面,允許用戶輸入他們想要訂購的數量。模型類如下
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; }
}
和
public class Item
{
public int CustomId { get; set; }
public Inventory Inventory { get; set; }
}
在QuantityReq多年平均值在資料庫中存在,所以我說他們作為NotMapped。所以我有一個視圖名稱AddtoOrder在專案上
@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 />
<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");}
}
控制器操作如下所示,如果用戶輸入的數量大于可用數量,則下訂單并導航回其他頁面。但是,如果用戶在所需數量中輸入的數字大于可用數量,那么我需要在他們輸入的數量無效的同一頁面中發布錯誤訊息
// 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){
How to redirect to the same page back with the validation error
}
}
uj5u.com熱心網友回復:
首先,您應該添加@Html.ValidationSummary(false, "", new { @class = "error" })到表單中。另外,我建議您使用HTML Helpers。
這是表單的簡單示例:
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)
@Html.LabelFor(m => m.Age)
@Html.TextBoxFor(m => m.Age)
<input type="submit" value="Submit"/>
@Html.ValidationSummary(false, "", new { @class = "error" })
}
然后您可以自定義驗證您的模型并將錯誤發送到視圖:
// Validation logic
else if (quantityReq > intData.QuantityAvailable)
{
ModelState.AddModelError("QuantityReq", "QuantityReq more than QuantityAvailable");
return View();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/343443.html
標籤:javascript 查询 阿贾克斯 asp.net核心 asp.net-ajax
上一篇:如何獲得每個會員的最快時間
