I have been trying to pass the quantity to my controller but cannot figure out how. I am new to this and need some help! I know that I need to make an ActionResult UpdateCart(int BookID, int quantity) but do not know what needs to go in it.
Here is my view code:
@{
ViewBag.Title = "Cart";
}
@using FinalProject_Lio_Lopez_Jafri_Wood.Controllers;
@model ShoppingCartItem
<h2>Cart</h2>
<table class="table table-hover">
<tr>
<th>Book Title</th>
<th>Book Unique Nnmber</th>
<th>Book Price</th>
<th>Quantity</th>
<th>Option</th>
<th>Sub Total</th>
</tr>
@{decimal s = 0;}
@foreach (ShoppingCartItem item in (List<ShoppingCartItem>)Session["cart"])
{
s += item.Books1.BookPrice * item.Quantity;
<tr>
<td>@item.Books1.BookTitle</td>
<td>@item.Books1.BookUniqueNumber</td>
<td>$ @item.Books1.BookPrice</td>
<td>@Html.TextBoxFor(m => m.Quantity, new { @Value = "1", @class = "form-control" , style="width:50px;" })</td>
<td>
@Html.ActionLink("Refresh Quantity", "Update", "ShoppingCart", new{id = item.Books1.BookID, quantity = item.Quantity}) | @Html.ActionLink("Remove Item", "Delete", "ShoppingCart",
new { id = item.Books1.BookID }, null)
</td>
<td>$ @(item.Books1.BookPrice * item.Quantity)</td>
</tr>
}
<tr>
<td align="right" colspan="5">TOTAL</td>
<td>$ @s</td>
</tr>
</table>
<br />
@Html.ActionLink("Continue Shopping", "Search", "Books")
<input type="button" value="Check Out" class="btn-info btn-active" style="float: right" />
Here is my controller code so far:
public class ShoppingCartController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult Index()
{
return View();
}
private int isExisting(int id)
{
List<ShoppingCartItem> cart = (List<ShoppingCartItem>) Session["cart"];
for (int i = 0; i < cart.Count; i++ )
if(cart[i].Books1.BookID==id)
return i;
return -1;
}
public ActionResult Delete(int id)
{
int index = isExisting(id);
List<ShoppingCartItem> cart = (List<ShoppingCartItem>)Session["cart"];
cart.RemoveAt(index);
Session["cart"] = cart;
return View("Cart");
}
public ActionResult UpdateCart(int BookID, int quantity)
{
return View("Cart");
}
public ActionResult AddToCart(int id)
{
if (Session["cart"] == null)
{
List<ShoppingCartItem> cart = new List<ShoppingCartItem>();
cart.Add(new ShoppingCartItem(db.Books.Find(id), 1));
Session["cart"] = cart;
}
else
{
List<ShoppingCartItem> cart = (List<ShoppingCartItem>) Session["cart"];
int index = isExisting(id);
if (index == -1)
cart.Add(new ShoppingCartItem(db.Books.Find(id), 1));
else
cart[index].Quantity++;
Session["cart"] = cart;
}
return View("Cart");
}
}
}