MVC2 Trying to pass multiple parameters to ActionResult based on textbox with dynamic name
I am trying to implement a quantity selector for the number of items added to a shopping cart.
I have a textbox with a dynamic name for each row of items in the catalog. Each row has a "Add to Cart" button.
If I enter the desired quantity in the textbox and click the "Add To Cart" button, I want the entered quantity of the selected item to be added to my Cart.
The controller action that adds the new quantity to the database for the cart is as follows:
public ActionResult AddToCart(int productID, int quant)
{
repository.AddItemToOrder(productID, quant);
return RedirectToAction("Browse");
}
I know that calling the "Browse" action to render the page again is not the most efficient method. I will account for that later.
My question is: How do I make a call to this "AddToCart" controller action with both parameters?
I was able to get a version working with 1 parameter. In this case the second parameter in the above controller action, quant, was removed.
The line in my View was:
<input type="button" onclick="document.location.href = '<%: Url.Action("AddToCart") %>'+'?productID=<%: item.ProductID %>' " value="Add to Cart" />
While this worked, I need to be able to reference the textbox for the quantity.
I tried this next line that includes multiple parameters in my View:
<input type="button" onclick="document.location.href = '<%: Url.Action("AddToCart") %>'+'?productID=<%: item.ProductID %>'+'?quant=<%: item.ProductID %>' " value="Add to Cart" />
The value assigned to "quant" is the same as what is assigned to "productID". I did this to simply get something to work correctly. Once it is working I plan to use the value of the textbox that has a dynamic name. (I need some help with that as well)
When I tested and clicked the button for the row, I received the following error:
The parameters dictionary contains a null entry for parameter 'productID' of 开发者_如何学JAVAnon-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult AddToCart2(Int32, Int32)' in 'OER.Controllers.eCommerceController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
You need an ampersand, &
, between variables in a querystring, not another ?
... simple typo.
/AddToCart?productId=3&quant=1
If you want to give quant
a default value, so it is not always required, you can change your action method to something like:
public ActionResult AddToCart(int productID, int quant = 1)
Based on your comments, I would set up a form that the button will submit, instead of squeezing it all into an onclick
:
@using(Html.BeginForm("AddToCart")) {
@Html.Hidden("productId", item.ProductId)
@Html.Input("quant")
<input type="submit" value="Add to Cart" />
}
If you want to switch to just a regular link instead of a button, you'll need to wire up some jQuery to pull the quant
parameter from the text box on click.
$('.addToCartLink').click(function() { // All the "add to cart" links would have this class
// Pull the product ID (just an example)
var productId = $(this).attr('data-id');
// Set the quantity
var quantity = $('#txtlinequantity-'+productId).val();
// Possible validation here
...
// Hit the action method
window.location = '/AddToCart?productId='+productId+'&quant='+quantity;
});
Don't you want
<input type="button" value="<%: item.ProductID %>" name="productID" id="productID"/>
inside the form you're submitting?
If you have a form element being submitted, MVC will automatically pull the required information and assign it to the equivalent variable in your code behind based on the name
field (or id
). The value
field is the value to assign to the corresponding variable.
精彩评论