视图:
<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);
});
如何发送标题和价格到动作结果?
最佳答案
ajax呼叫:
$.ajax({
url:'@(Url.Action("SomeAction", "Some"))',
data: {title:title, price:price},
success:function(data){
//data is response from action
....
}
});
动作本身:
public class SomeController: Controller
{
public ActionResult SomeAction(string title, string price)
{
...
}
}
并使用text()而不是html(),否则如果您将html代码发送给行动,则会收到危险请求的异常:
var title = container.find('.item-dtls > h4 > a').text();
var price = container.find('.price').text();