asp.net mvc - Sending data from View to another Controller using JQuery -


i want send data view controller using jquery

var title; var price; var container; $('a.btn').click(function () {     container = $(this).closest('.item');     title = container.find('.item-dtls > h4 > a').text();     price = container.find('.price').text();  });  $('a.btn').click(function () {     $.ajax({         url: '@(url.action("item", "home"))',         type:'post',         datatype:text,         data:{title,price},         success: function (data) {             alert('success');         },         error: function (data) {             alert('error');         }     }); }); 

this controller

[httppost] public actionresult item(string title,string price) {     response.write(title); } 

send data name-value pairs. not need 2 separate click event handlers.

also, datatype property value should string. wrap in single/double quotes.

$('a.btn').click(function () {      var container = $(this).closest('.item');       var titleval = container.find('.item-dtls > h4 > a').text();      var priceval = container.find('.price').text();        alert(titleval);      alert(priceval);      $.ajax({          url: '@url.action("item", "home")',          type:'post',          datatype:'text',          data: { title : titleval , price : priceval },          success: function (data) {              alert('success');              alert(data);          },          error: function (data) {              alert('error'); }          });      });  }); 

also, cannot use response.write return action method. can use content method return string.

[httppost] public actionresult item(string title, string price) {             return content(title); } 

Comments