I currently have a View which retrieves a list from a view model and displays a table for each item inside it. Now, what I want to do is the following: every time a user clicks to the button I've displayed, the application makes a POST request sending the specific item related and storing it, with its own quantity, to the current Session. To clarify, here is my code:
ProductLists.cshtml
@model B2BCommerce.Models.ViewModels.OrderViewModel
@Styles.Render("~/Content/bootstrap.css")
@{
short indexSelected = 0;
short i = 0;
ViewBag.Title = "Product List";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<style>
.center-table {
margin: 0 auto !important;
float: none !important;
}
</style>
<h2><center>Shop</center></h2>
<hr/>
<div class="container">
<table class="table table-hover center-table">
@foreach (B2BCommerceThesis.Models.ViewModels.ShoppingItem item in Model.shoppingItems)
{
using (Html.BeginForm("AddToShoppingCart", "Order", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken();
<tr>
<td rowspan="3"> PhotoName </td>
<td colspan="3"> <center>@Html.LabelFor(m => m.shoppingItems[i].p.Name) </center> </td>
<td rowspan="3">Description</td>
</tr>
<tr>
<td colspan="3">@item.c.CompanyName</td>
</tr>
<tr>
<th>@(item.p.Price + " " + item.CurrencyName)</th>
<th>@item.CategoryName</th>
<th>
@if (item.p.Weight!= null)
{
if (item.p.Length!= null)
{
@("Peso: " + item.p.Weight+ item.WeightName + "<br>Length: " + item.p.Length+ item.LengthName)
}
else
{
@("Peso: " + item.p.Weight+ item.WeightName )
}
}
</th>
</tr>
<tr>
<th colspan="2"></th>
<th>
@Html.DropDownListFor(m => m.shoppingItems[i].SelectedQuantity, new SelectList(item.QuantitySelectedItem, "selectedQuantity"))
</th>
<th><button name="insertButton" class="btn btn-info">Add to cart </button></th>
</tr>
<tr>
<input type="hidden" value="@i" id="indexElement" name="indexElement"/>
</tr>
@{ i++; }
}
}
</table>
</div>
OrderViewModel.cs
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace B2BCommerce.Models.ViewModels
{
public class ShoppingItem
{
public Product p;
public Customer c;
public short SelectedQuantity { get; set; }
public List<short> SelectedQuantities{ get; set; }
public string WeigthName{ get; set; }
public string LengthName { get; set; }
public string CurrencyName { get; set; }
public string CatwgoryName { get; set; }
public ShoppingItem()
{
p = new Product();
c = new Customer();
QuantitaProdottiSelezionati = new List<short>();
}
}
public class SessionShoppingCart
{
public List<ShoppingItem> ShoppingCartItems { get; set; }
public List<short> Quantities { get; set; }
public SessionShoppingCart()
{
ShoppingCartItems = new List<ShoppingItem>();
Quantities = new List<short>();
}
}
}
OrderController.cs
public ActionResult AddToShoppingCart(ShoppingItem item)
{
short indexItem = Convert.ToInt16(Request["indexElement"].ToString());
Session["Products"] = item;
Session["Quantities"] = item.SelectedQuantity;
return RedirectToAction("ProductList");
}
public ActionResult ProductList()
{
OrderViewModel vm = new OrderViewModel();
vm.Load();
return View(vm);
}
public ActionResult ShoppingCartDetail()
{
SessionShoppingCart sc = new SessionShoppingCart();
sc.ShoppingCartItems = (List<ShoppingItem>)Session["Products"];
sc.Quantities = (List<short>)Session["Quantities"];
return View(sc);
}
So, my question is: is there anyway to send a single shoppingItem object, related to the button pressed, as a parameter since I've populated this table dynamically?