Thamsanqa Ngcongwane

Thamsanqa Ngcongwane

  • NA
  • 184
  • 23.1k

How to Pass data from Ajax to Controller method ?

Jun 18 2020 1:11 PM
Hi Everyone,
 
I have the following javascript code:
  1. <script>  
  2. function RequestData() {  
  3. var Nos = $("#search").val();  
  4. var DataIntervals = $("#txtDataInterval").val();  
  5. var StartDates = $("#start_date").val();  
  6. var EndDates = $("#end_date").val();  
  7. if (Nos != null && DataIntervals != null && StartDates != null && EndDates != null)  
  8. {  
  9. $.ajax({  
  10. type: "POST",  
  11. async: false,  
  12. url: 'ExtractData/ExtractData',  
  13. data: { 'No': Nos, 'DataInterval': DataIntervals, 'StartDate': StartDates, 'EndStart': EndDates},  
  14. contentType: "application/json; charset=utf-8",  
  15. dataType: "json",  
  16. success: function (msg) {  
  17. alert("The Query Has Completed Successfully.");  
  18. },  
  19. error: function (e) {  
  20. alert("Something Went Wrong Please Try Again." + e);  
  21. }  
  22. });  
  23. else  
  24. {  
  25. alert("Please Select All The Required Fields and Try Again.");  
  26. }  
  27. }  
  28. </script>  
I'm trying to pass to parameters to C# method but they always null.
 
Here is my C# method:
  1. [HttpPost]  
  2. public IActionResult ExtractData(string No, string DataInterval, string StartDate, string EndStart)  
  3. {  
  4. string x = No;  
  5. return View();  
  6. }  
What is it I'm doing wrong that makes this method parameters to always be null?

Answers (2)