Get ViewData IList back on postback in ASP.NET MVC
- by Thomas
I have the following in my view:
<%using (Html.BeginForm()) {%>
<% foreach (var item in Model.Cart) { %>
<div>
<%= Html.TextBox("Cart.Quantity", item.Quantity, new { maxlength = "2" })%>
<%= Html.Hidden("Cart.ItemID", item.ItemID)%>
</div>
<% } %>
<input name="update" type="image" src="image.gif" />
<% } %
I then have this code in my controller:
public ActionResult Index()
{
CartViewData cartViewData = new CartViewData();
IList<Item> items = ItemManager.GetItems();
cartViewData.Cart = items;
return View("Index", cartViewData);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(CartViewData cartViewData)
{
// cartviewData is null
}
Is there a way to grab the List on the postback to see if the values in the textboxes have changed?
Thanks
Below is a simplified example since it was requested:
<% for (int i = 0; i < Model.Cart.Count; i++ ) { %>
<a href="javascript:void(0);" onclick="removeItem(<%= Model.Cart[i].ShoppingCartItemID %>);">Remove</a>
<% } %>
Hope this helps.