I am building an Product Ordering web application I have used Entity Framework to Scaffold-DbContext to Scaffold the Tables in the DB to the Model Classes. I have table called Inventory in the DB and now the view is created in the .Net Core project that shows the list of the inventory in the DB like below
I am trying to add a new field called QuantityRequired in the Model class that is not needed to be on the the Database. On the view I am trying to make this an input field so the users can enter the no.of items required and click Buy Now that add the item into the cart
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; }
}
Upon reading I found that the [NotMapped]
attribute helps with this, so updated the model class like above and the view is now like
<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>
Now it shows the new field in the table
I did not do anything on the Controller of the Inventory Page.Should I bind this value in the controller? Because when the button Buy Now is clicked I have the below code on the 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? inventoryID)
{
if (customerID == null || inventoryID == 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");
}
}
I tried to retrieve the QuantityReq from the Quantity = intData.QuantityReq
but it shows ‘0’ and not the value that was entered by the users
Cart View page is like
@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>
}
How to pass the user entered value from the Inventory page into the Cart page
2
Answers
You’re not sending the quantity to the Controller for it to process when performing the Buy.
Instead, you’re retrieving an object "intData" from the database (which clearly will not have the value that you just entered in the UI), and then trying to use Quantity from that intData to populate an item in the cart.
You need to add quantity as a parameter on your controller method, send the value from the UI, and then use that parameter when creating the cart item:
Client UI:
Controller:
this code was tested in Visual Studio
and remove [Route("buy/{customerID}/{invetoryID}/{quantityReq}")] from action