ajax keypress function is not working and the ActionResult parameter is not receiving any value


ajax keypress function is not working and the ActionResult parameter is not receiving any value



I want to change(calculate) a textboxs' value on ajax keypress event but the controller ActionResult is not receiving any value to calculate(receiving null)


<script>
$('#TotDiscnt').keypress(function () {
//var data = $('#totDiscnt').val();
$.ajax({
type: 'Post',
url: '/Whatever/Discount',
data: $('#totDiscnt').val(),
success: function (response) {
$('#TotPurAmt').val(response);
}
});
});
</script>



On controller


public ActionResult Discount(string text)
{

// calculation

return Json(sum, JsonRequestBehavior.AllowGet);



I tried as



data: { text: request.term },



while i do that the the ajax call is not calling the controller method.And one more thing how can i send two double value as a parameter to the ActionResult method form an ajax call as


<script>
$('#TotDiscnt').keypress(function () {
//var data = $('#totDiscnt').val();
$.ajax({
type: 'Post',
url: '/Whatever/Discount',
data: {num1:$('#totDiscnt').val(),
num2:$('#Discnt').val() },
success: function (response) {
$('#TotPurAmt').val(response);
}
});
});




and then receive as double value in parameter like


public ActionResult Discount(double num1, double num2)
{
//calculation

return Json(sum, JsonRequestBehavior.AllowGet);




2 Answers
2



Yes you can add event either keyup or keypress. You can change your code slightly.


<script>
$('#TotDiscnt').keypress(function () {
//var data = $('#totDiscnt').val();
$.ajax({
type: 'POST',
contentType: 'application/json; charset=UTF-8', //send type of data to sever
dataType: 'json', //retrun type of data from server
url: '/Whatever/Discount',
data: JSON.stringify(text:$(this).val()),

// data: $('#totDiscnt').val(),
success: function (response) {
$('#TotPurAmt').val(response);
}
});
});





Here your controller should be as.


public ActionResult Discount(string text)
{

// calculation

return Json(sum, JsonRequestBehavior.AllowGet);
}



Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
For more detail please visit official site click me



Please let know in comment is it working or not.





Is this line of code in correct format "data: {json.stringify( text:$('#totDiscnt').val())},"? after json. it is saying " : expected ", text: " ) expected" and before } it is saying " ; expected"
– Raihan Ridoy
Jul 1 at 7:06





and the ajax is not sending a request at server
– Raihan Ridoy
Jul 1 at 7:08





Ahan , ok remove curly braces , code updated
– Asif Raza
Jul 1 at 7:09





Write inside currly braces.
– Asif Raza
Jul 1 at 7:21





Happy coding . . First try urself.
– Asif Raza
Jul 1 at 7:50



To send multiple value from an ajax call to ActionResults' parameter:
Script:


<script>
$(document).ready(function () {
$('#btnCalculate').click(function () {
$.ajax({
type: 'Post',
url: '/Sales/Calculate',
data: { num1: $('#txtNum1').val(), num2: $('#txtNum2').val(),//so on.... },
success: function (response) {
$('#txtSum').val(response)
}
});
});
});
</script>



and on controller:


public ActionResult Calculate(int num1, int num2)
{
int sum = 0;
sum = num1 + num2;
return Json(sum, JsonRequestBehavior.AllowGet);
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API