Get ViewData IList back on postback in ASP.NET MVC

Posted by Thomas on Stack Overflow See other posts from Stack Overflow or by Thomas
Published on 2009-10-22T01:09:37Z Indexed on 2010/04/19 7:23 UTC
Read the original article Hit count: 269

Filed under:

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.

© Stack Overflow or respective owner

Related posts about asp.net-mvc