Ansar Maleki
Ansar Maleki

Reputation: 41

How to send result of Jquery to controller

View:

<div class="item">
<!-- Item's image -->
<img class="img-responsive" src="http://lorempixel.com/200/200/food/1/" alt="">
<!-- Item details -->
<div class="item-dtls">
<!-- product title -->
<h4><a href="#" >Lorem product</a></h4>
<!-- price -->
<span class="price lblue"  >$23.00</span>
</div>
<!-- add to cart btn -->
<div class="ecom bg-lblue">
<a class="btn" href="#" ">Add to cart</a>
</div>
</div>

Jquery:

$('a.btn').click(function () {
var container = $(this).closest('.item');
var title = container.find('.item-dtls > h4 > a').html();
var price = container.find('.price').html();
alert('Title: ' + title + ', Price: ' + price);
});

How to send title and price to action result ?

Upvotes: 2

Views: 56

Answers (1)

Gene R
Gene R

Reputation: 3744

ajax call:

$.ajax({
    url:'@(Url.Action("SomeAction", "Some"))',
    data: {title:title, price:price},
    success:function(data){
        //data is response from action
        ....
    }
});

Action itself:

public class SomeController: Controller
{
    public ActionResult SomeAction(string title, string price)
    {
        ...
    }
}

And use text() insteadof html(), or you get exception of dangerous request if u send html code to action:

var title = container.find('.item-dtls > h4 > a').text();
var price = container.find('.price').text();

Upvotes: 2

Related Questions