Saturday, 17 August 2013

How to send a model property value from a view to controller

How to send a model property value from a view to controller

I have a view tied with a Item model that displays a shopping item. I want
to a add a quantity text field and submit the itemID and quantity to a
controller. I am using an AJAX form.
AJAX form:
@using (Ajax.BeginForm("AddToCart", "PizzaBasket",
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
HttpMethod = "GET",
OnFailure = "searchFailed",
LoadingElementId = "ajax-loader",
UpdateTargetId = "basketSummary",
}))
{
@Html.HiddenFor(id => id.ItemId, new { @class = "id" })
<input type="hidden" name="id" class="id")/>
<label for="quantity">Quantity</label>
<input type="text" name="quantity" size="5"/>
<input type="submit" value="Add to Cart" />
<img id="ajax-loader"
src="@Url.Content("~/Content/Images/ajax-loader.gif")"
style="display:none"/>
}
Controller action:
public ActionResult AddToCart(String id, String quantity) {
/*Add to cart*/
return RedirectToAction("Index");
}
However, the id value is not getting submitted. Need to find out how to
pass the id value from the AJAX form to the controller. Please note, that
in the same file @Model.itemId is working fine.

No comments:

Post a Comment