我正在構建一個產品訂購 Web 應用程式我已經使用物體框架到腳手架-DbContext 將資料庫中的表腳手架到模型類。我在 DB 中有一個名為 Inventory 的表,現在在 .Net Core 專案中創建了視圖,該專案顯示了 DB 中的庫存串列,如下所示

我正在嘗試在模型類中添加一個名為 QuantityRequired 的新欄位,該欄位不需要在資料庫中。在視圖中,我試圖將其設定為輸入欄位,以便用戶可以輸入所需的商品數量,然后單擊“立即購買”將商品添加到購物車中
public partial class Inventory
{
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 string RoomNumber { get; set; }
public int InventoryId { get; set; }
[NotMapped]
public int QuantityRequired { get; set; }
}
閱讀后我發現 [NotMapped] 屬性對此有所幫助,因此更新了上面的模型類,現在視圖就像
<td >
<input id="Text3" type="text" asp-for="@item.QuantityReq" />
</td>
<td>
<a asp-controller="cart" asp-action="buy" asp-route-customerID="@custID.CustomerId" asp-route-invetoryID="@item.InventoryId">Buy Now</a>
</td>
現在它顯示表中的新欄位

我沒有在庫存頁面的控制器上做任何事情。我應該在控制器中系結這個值嗎?因為當單擊“立即購買”按鈕時,我在 CartController 上有以下代碼
public IActionResult Index()
{
var cart = SessionHelper.GetObjectFromJson<List<Item>>(HttpContext.Session, "cart");
ViewBag.cart = cart;
return View();
}
[Route("buy/{customerID}/{invetoryID}")]
public async Task<IActionResult> Buy(int? customerID, int? invetoryID)
{
if (customerID == null || invetoryID == null)
{
return NotFound();
}
Customer custData = await _context.Customers.FindAsync(customerID);
var intData = await _context.Inventories.FindAsync(invetoryID);
if (SessionHelper.GetObjectFromJson<List<Item>>(HttpContext.Session, "cart") == null)
{
List<Item> cart = new List<Item>();
cart.Add(new Item
{
Custom = custData,
Inventory = intData,
**Quantity = intData.QuantityReq**
});
SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
}
return RedirectToAction("Index");
}}}
我試圖從 檢索 QuantityReqQuantity = intData.QuantityReq**但它顯示“0”而不是用戶輸入的值

購物車查看頁面就像
@foreach (var item in ViewBag.cart)
{
<tr>
<td>@item.Inventory.StrainId</td>
<td>@item.Inventory.StrainName</td>
<td>@item.Inventory.StrainCode</td>
<td>@item.Inventory.Age</td>
<td>@item.Inventory.Sex</td>
<td>@item.Quantity</td>
</tr>
}
如何將用戶輸入的值從庫存頁面傳遞到購物車頁面
uj5u.com熱心網友回復:
在執行購買時,您不會將數量發送給控制器以供其處理。
相反,您正在從資料庫中檢索一個物件“intData”(它顯然沒有您剛剛在 UI 中輸入的值),然后嘗試使用該 intData 中的 Quantity 來填充購物車中的專案。
您需要在控制器方法上添加數量作為引數,從 UI 發送值,然后在創建購物車專案時使用該引數:
客戶端界面:
<td >
<input id="Text3" type="text" asp-for="@item.QuantityReq" />
</td>
<td>
<a asp-controller="cart"
asp-action="buy"
asp-route-customerID="@custID.CustomerId"
asp-route-invetoryID="@item.InventoryId"
asp-route-quantity="@item.QuantityReq">Buy Now</a>
</td>
控制器:
[Route("buy/{customerID}/{invetoryID}/{quantityReq}")]
public async Task<IActionResult> Buy(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 (SessionHelper.GetObjectFromJson<List<Item>>(HttpContext.Session, "cart") == null)
{
List<Item> cart = new List<Item>();
cart.Add(new Item
{
Custom = custData,
Inventory = intData,
Quantity = quantityReq
});
SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
}
return RedirectToAction("Index");
}
uj5u.com熱心網友回復:
此代碼在 Visual Studio 中進行了測驗
@foreach (var item in ViewBag.cart)
{
<tr>
<td>@item.Inventory.StrainId</td>
<td>@item.Inventory.StrainName</td>
<td>@item.Inventory.StrainCode</td>
<td>@item.Inventory.Age</td>
<td>@item.Inventory.Sex</td>
<td>@item.Quantity</td>
<td>
<form method="post"
asp-controller="cart"
asp-action="buy">
<row>
<column>
<input type="text" id="quantityReq" name="quantityReq" value = @item.QuantityReq/>
</column>
<column>
<input type="hidden" id="customerID" name="customerID" value = "@custID.CustomerId" />
<input type="hidden" id="invetoryID" name="invetoryID" value = "@item.InventoryId" />
<button type="submit" style="border:none; background-color:transparent"> <u> Buy now</u> </button>
</column>
</row>
</form>
</td>
</tr>
}
并從操作中洗掉 [Route("buy/{customerID}/{invetoryID}/{quantityReq}")]
public async Task<IActionResult> Buy(int customerID, int invetoryID, int quantityReq)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/332683.html
標籤:asp.net 实体框架 asp.net核心 实体框架核心 asp.net-core-mvc
