Abraham Olatubosun

Abraham Olatubosun

  • NA
  • 471
  • 107.4k

JQUERY AJAX NOT WORKING IN ASP.NET CORE MVC

Mar 3 2020 6:44 PM
Hello,
I hope my question meet you all in good health and happiness.
 
I am writting an application for my team mates to help correct items in the database, the program is in ASP.NET Core using Jquery Ajax at the front end to call the controller methods; when i execute application, as soon as it get to the Ajax call the execution jump to the end of the section it didn't go through the Ajax function and the controller method never get to be called.
 
I don't know what i am doing wrong, below are the codes
 
This the Layout:
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="utf-8" />  
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0" />  
  6.     <title>@ViewData["Title"] - DRUG_REPLACEMENT</title>  
  7.   
  8.     <link href="~/Dis/css/bootstrap.css" rel="stylesheet" />  
  9.     <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">  
  10.   
  11.   
  12.     <script src="~/Dis/js/jquery-3.4.1.min.js"></script>  
  13.     <script src="~/Dis/js/bootstrap.js"></script>  
  14.     <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">  
  15.     <script src="https://code.jquery.com/jquery-1.12.4.js"></script>  
  16.     <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>  
  17.   
  18. </head>  
  19. <body>  
  20.   
  21.     <div class="container">  
  22.         <main role="main" class="pb-3">  
  23.             @RenderBody()  
  24.         </main>  
  25.     </div>  
  26.      
  27. </body>  
  28. </html>  
 This is Client front:
  1. @{  
  2.     ViewData["Title"] = "Home Page";  
  3. }  
  4.   
  5.   
  6.     <div>  
  7.   
  8.         <div>Nav Bar</div>  
  9.         <br />  
  10.   
  11.         <div class="row">  
  12.             <div class="col-2">  
  13.   
  14.             </div>  
  15.             <div class="col-6">  
  16.                 <table class="table table-bordered table-responsive-md">  
  17.                     <tr>  
  18.                         <td colspan="3" class="text-center bg-secondary">  
  19.                             <h3><strong>Drug Replacement </strong></h3>  
  20.                         </td>  
  21.                     </tr>  
  22.                     <tr>  
  23.                         <td colspan="3">  
  24.   
  25.                             # of patient with EFV:<label id="EFV"></label><br />  
  26.                             # of patient with NVP :<label id="NVP"></label>  
  27.                         </td>  
  28.                     </tr>  
  29.                     <tr>  
  30.                         <td>  
  31.                             Found within <input type="text" id="d1" class="form-control" readonly="readonly" placeholder="click for date" />  
  32.                             TO <input type="text" id="d2" class="form-control" readonly="readonly" placeholder="click for date" />  
  33.                         </td>  
  34.                     </tr>  
  35.                     <tr>  
  36.                         <td>  
  37.                             <button id="btn-process" type="submit" class="btn btn-success btn-block">Process</button>  
  38.                         </td>  
  39.   
  40.                     </tr>  
  41.   
  42.                 </table>  
  43.   
  44.   
  45.   
  46.             </div>  
  47.             <div class="col-2"></div>  
  48.   
  49.         </div>  
  50.   
  51.         <script type="text/javascript">  
  52.   
  53.             $(document).ready(  
  54.                 function ()  
  55.                 {  
  56.                     $('#d1').datepicker({  
  57.                         changeMonth: true,  
  58.                         changeYear: true,  
  59.                         dateFormat: 'dd/mm/yy'  
  60.                     });  
  61.                     $('#d2').datepicker({  
  62.                         changeMonth: true,  
  63.                         changeYear: true,  
  64.                         dateFormat: 'dd/mm/yy'  
  65.                     });  
  66.   
  67.                     showRegimen();  
  68.                 }  
  69.             )  
  70.             function showRegimen() {  
  71.   
  72.                 $('#NVP').html(0);  
  73.                 $('#EFV').html(0);  
  74.   
  75.                 $.ajax({  
  76.                     URL: "/api/Home/ReplaceTLD?str=12",  
  77.                     method: "POST",  
  78.                     dataType: "Json",  
  79.                     success: function (data) {  
  80.                         console.log(data);  
  81.   
  82.                         if (data.errCode > 0) {  
  83.                             $('#NVP').html(data.itemCount.nvp);  
  84.                             $('#EFV').html(data.itemCount.efv);  
  85.                         }  
  86.                         else {  
  87.                             console.log(data.errMsg);  
  88.                         }  
  89.                     },  
  90.                     error: function (e) {  
  91.                         console.log(e);  
  92.                     }  
  93.                 });  
  94.             }  
  95.   
  96.             $('#btn-process').click(function () {  
  97.                 correctRegimens();  
  98.             });  
  99.   
  100.             //?FrmDates=' + str1 + '&ToDates=' + str2 + ''  
  101.             function correctRegimens() {  
  102.                 var DDates = new Object();  
  103.                 DDates.fromDate = $('#d1').val();  
  104.                 DDates.toDate = $('#d2').val();  
  105.   
  106.                 var str1 = $('#d1').val();  
  107.                 var str2 = $('#d2').val();  
  108.   
  109.                 if (str1 != null && str2 != null) {  
  110.   
  111.                     $('#btn-process').html('Please wait...');  
  112.                     $('#btn-process').prop('disabled', false);  
  113.   
  114.                     var URLs = '/api/Home/correctRegimen';  
  115.   
  116.                     $.ajax({  
  117.                         method: "POST",  
  118.                         URL: URLs,  
  119.                         dataType: "Json",  
  120.                         data: JSON.stringify(DDates),  
  121.                         success: function (data) {  
  122.                             console.log(data);  
  123.   
  124.                             if (data != null && data.errCode > 0) {  
  125.                                 $('#btn-process').html('Process');  
  126.                                 $('#btn-process').prop('disabled', false);  
  127.                             }  
  128.   
  129.                         },  
  130.                         error: function () {  
  131.                             alert("Error occured!!")  
  132.                         }  
  133.   
  134.                     });  
  135.   
  136.                 }  
  137.   
  138.             }  
  139.   
  140.   
  141.         </script>  
  142.   
  143.   
  144.   
  145.   
  146.   
  147.     </div>  
This the Model Class:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5.   
  6. namespace DRUG_REPLACEMENT.Models  
  7. {  
  8.     public class LoadTLD  
  9.     {  
  10.         public class FromTo  
  11.         {  
  12.             public DateTime FrmDate { getset; }  
  13.             public DateTime ToDate { getset; }  
  14.         }  
  15.         public class ItemCount  
  16.         {  
  17.             public int EFV { getset; }  
  18.             public int NVP { getset; }  
  19.         }  
  20.         public class ErrMsg  
  21.         {  
  22.             public int errCode { getset; }  
  23.             public string errMsg { getset; }  
  24.             public ItemCount ItemCount { getset; }  
  25.             public FromTo FromTo { getset; }  
  26.         }  
  27.     }  
  28. }  
 And this the Controller methods:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.AspNetCore.Mvc;  
  6. using Microsoft.Extensions.Configuration;  
  7. using System.Data;  
  8. using MySql.Data.MySqlClient;  
  9. using Newtonsoft.Json;  
  10. using static DRUG_REPLACEMENT.Models.LoadTLD;  
  11.   
  12. namespace DRUG_REPLACEMENT.Controllers  
  13. {  
  14.     public class HomeController : Controller  
  15.     {  
  16.   
  17.         public int NVP1;  
  18.         public int EFV1;  
  19.         /* 
  20.          * This add dependency injection to read the 
  21.          * ConnectionStrings from appsettings 
  22.          * */  
  23.         private readonly IConfiguration configuration;  
  24.         public HomeController(IConfiguration config)  
  25.         {  
  26.             this.configuration = config;  
  27.         }  
  28.   
  29.         public ActionResult Index()  
  30.         {  
  31.             return View();  
  32.         }  
  33.         public ActionResult ReplaceTLD()  
  34.         {  
  35.             return View();  
  36.   
  37.         }  
  38.   
  39.         [HttpPost]  
  40.         public ActionResult<ErrMsg> ReplaceTLD(string str)  
  41.         {  
  42.             ErrMsg msg = new ErrMsg();  
  43.   
  44.             try  
  45.             {  
  46.                 var con = configuration.GetConnectionString("NMRSCon");  
  47.                 using (MySqlConnection cn = new MySqlConnection(con))  
  48.                 {  
  49.                     cn.Open();  
  50.   
  51.                     var Sql = string.Concat("DROP TABLE IF EXISTS temp_regimen;",  
  52.                       "CREATE TABLE temp_regimen AS ",  
  53.                       "SELECT p.patient_id, pid.identifier, IF(obs.concept_id = 164506, cn1.name, NULL) AS regimen, obs.Value_coded,",  
  54.                       "IF(obs.concept_id = 164506, obs.obs_datetime, NULL) AS Obs_Date  FROM    (",  
  55.                       "(SELECT * FROM patient) AS p ",  
  56.                       " LEFT JOIN ",  
  57.                       " (SELECT * FROM patient_identifier) AS pid ON p.patient_id = pid.patient_id ",  
  58.                       " LEFT JOIN ",  
  59.                       " (SELECT * FROM Obs WHERE obs.concept_id = 164506 AND obs.value_coded = 164505 OR obs.Value_coded = 162565) AS obs ",  
  60.                       " ON pid.patient_id = obs.person_id ",  
  61.                       " LEFT JOIN ",  
  62.                       "  concept_name AS cn1 ON(obs.value_coded = cn1.concept_id AND cn1.locale = 'en' AND cn1.locale_preferred = 1) )");  
  63.                     //" WHERE TIMESTAMP(obs.obs_datetime) BETWEEN TIMESTAMP('2019/06/01') AND CURDATE()");  
  64.   
  65.                     MySqlCommand cmd = new MySqlCommand(Sql, cn);  
  66.                     cmd.ExecuteNonQuery();  
  67.                     cmd.Dispose();  
  68.                     cn.Close();  
  69.   
  70.                     if (cn.State == ConnectionState.Closed)  
  71.                         cn.Open();  
  72.                     var SQLNV = "SELECT COUNT(regimen) as regCount FROM temp_regimen WHERE regimen ='TDF-3TC-NVP'";  
  73.                     MySqlCommand Ncmd = new MySqlCommand(SQLNV, cn);  
  74.                     NVP1 = Convert.ToInt32(Ncmd.ExecuteScalar());  
  75.   
  76.                     var SQLEFV = "SELECT COUNT(*) from temp_regimen where regimen ='TDF-3TC-EFV'";  
  77.                     MySqlCommand FVcmd = new MySqlCommand(SQLEFV, cn);  
  78.                     EFV1 = Convert.ToInt32(FVcmd.ExecuteScalar());  
  79.   
  80.                     msg.ItemCount = new ItemCount  
  81.                     {  
  82.                         EFV = EFV1,  
  83.                         NVP = NVP1  
  84.                     };  
  85.                     msg.errCode = 5;  
  86.                     msg.errMsg = "Process completed successfully. Please check the output for feedback";  
  87.                     return msg;  
  88.                 }  
  89.             }  
  90.             catch (Exception e)  
  91.             {  
  92.                 msg.errMsg = e.InnerException != null ? e.InnerException.Message : e.Message;  
  93.                 msg.errCode = -1;  
  94.                 return msg;  
  95.             }  
  96.   
  97.         }  
  98.   
  99.         [HttpPost]  
  100.         public ActionResult<ErrMsg> correctRegimen(string FrmDates, string ToDates)  
  101.         {  
  102.             ErrMsg msg = new ErrMsg();  
  103.             try  
  104.             {  
  105.                 msg.FromTo = new FromTo  
  106.                 {  
  107.                     FrmDate = Convert.ToDateTime(FrmDates.Trim()),  
  108.                     ToDate = Convert.ToDateTime(ToDates.Trim())  
  109.                 };  
  110.                 // TDF-3TC-DTG Concept is 165681  
  111.                 var USQL = string.Concat("UPDATE obs SET obs.Value_coded = 165681 where obs.Value_coded = 164505 OR ",  
  112.                     "obs.Value_coded = 162565 AND TIMESTAMP(obs.obs_datetime) BETWEEN TIMESTAMP('" + msg.FromTo.FrmDate + "') ",  
  113.                     "AND TIMESTAMP('" + msg.FromTo.ToDate + "') ");  
  114.                 msg.errMsg = USQL;  
  115.                 msg.errCode = 5;  
  116.                 return msg;  
  117.             }  
  118.             catch (Exception e)  
  119.             {  
  120.                 msg.errMsg = e.InnerException != null ? e.InnerException.Message : e.Message;  
  121.                 msg.errCode = -1;  
  122.                 return msg;  
  123.             }  
  124.   
  125.         }  
  126.   
  127.     }  
  128. }  
I need any suggestions and solution.
 
Thank you all 
 
 

Answers (9)