0

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");
        }
    }
}
Nikolay
  • 10,752
  • 2
  • 23
  • 51
Gaby Lio
  • 45
  • 4
  • I think this is what you are looking for http://stackoverflow.com/questions/5593759/actionlink-with-multiple-parameters – Venkata Krishna Reddy Nov 28 '15 at 17:11
  • You are rendering quantity as text box; does that mean the value entered in input control has to pass to action? A question here - Why are you creating your Update action as HttpGet, you should be creating as HttpPost and from view you should create a input type=submit to submit the server – user1672994 Nov 28 '15 at 18:18
  • You need a form, and post the form to a POST method. –  Nov 29 '15 at 03:28

1 Answers1

0

You seem to have some syntax issues. I would recommend you use resharper, it should underscodre at least such syntax errors.

  1. You have different action names in your controller and view. MVC is not (yet) smart enough to figure out that "Update" and "UpdateCard" is the same thing.

  2. You have another naming issue. Default MVC routing convention is to use id if you want the parameter to be part of URL and did not change routing. That's configurable, but the default is that.

  3. Check parameters of the ActionLink. You should have specified routing parameters, but it seems you specify html attributes. Check the declaration of the Html.ActionLink. Note that to be sure you can always use named parameters.

  4. Updates must never be doen with GET (like you do), as this provokes unwanted or random updates. Imagine for example a search engine (google) indexing your site - it will "click" your link (navigate to that) and this will add goods to the cart. Not good. I suggest you check some starter course on asp.net mvc...

Anyways, to fix the things, try:

Controller:

public ActionResult UpdateCart(int id, int quantity)
{
    return View("Cart");
}

View:

@Html.ActionLink("Refresh Quantity", "UpdateCart", "ShoppingCart", 
    new { id = item.Books1.BookID, quantity = item.Quantity}, null)
Nikolay
  • 10,752
  • 2
  • 23
  • 51