Question Background:
I am trying to pass two parameters (product name and price) from a View to an 'AddToCart' method on my MVC Controller. I'm doing this by passing the parameters to a JQuery Ajax function.
Issue:
Currently when clicking the 'Add To Cart' button 'link' the script is not being triggered. There is no error but the process is not taking place.
Here is the code:
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
//View:
<h3 class="panel-title boldFeatures" id="name">@((ViewBag.data as List<LoginTest.Models.HomePageItem>).First().ProductName)</h3>
<p id="price">@((ViewBag.data as List<LoginTest.Models.HomePageItem>).First().productPrice)</p>
<a class="btn btn-primary btn-block" id="addToCart">Test</a>
//Script:
<script>
$("#addToCart").on('click',function (e) {
e.preventDefault();
$.ajax({
url: '/HomePage/AddToCart',
data: { name: $('#name').text(), qty: $('#price').text() },
});
});
</script>
//Method on controller:
public void AddToCart(string name, string qty)
{
//Logic for adding to the cart.
}