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:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>@ViewData["Title"] - DRUG_REPLACEMENT</title>
-
- <link href="~/Dis/css/bootstrap.css" rel="stylesheet" />
- <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
-
-
- <script src="~/Dis/js/jquery-3.4.1.min.js"></script>
- <script src="~/Dis/js/bootstrap.js"></script>
- <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
- <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
- <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
-
- </head>
- <body>
-
- <div class="container">
- <main role="main" class="pb-3">
- @RenderBody()
- </main>
- </div>
-
- </body>
- </html>
This is Client front:- @{
- ViewData["Title"] = "Home Page";
- }
-
-
- <div>
-
- <div>Nav Bar</div>
- <br />
-
- <div class="row">
- <div class="col-2">
-
- </div>
- <div class="col-6">
- <table class="table table-bordered table-responsive-md">
- <tr>
- <td colspan="3" class="text-center bg-secondary">
- <h3><strong>Drug Replacement </strong></h3>
- </td>
- </tr>
- <tr>
- <td colspan="3">
-
- # of patient with EFV:<label id="EFV"></label><br />
- # of patient with NVP :<label id="NVP"></label>
- </td>
- </tr>
- <tr>
- <td>
- Found within <input type="text" id="d1" class="form-control" readonly="readonly" placeholder="click for date" />
- TO <input type="text" id="d2" class="form-control" readonly="readonly" placeholder="click for date" />
- </td>
- </tr>
- <tr>
- <td>
- <button id="btn-process" type="submit" class="btn btn-success btn-block">Process</button>
- </td>
-
- </tr>
-
- </table>
-
-
-
- </div>
- <div class="col-2"></div>
-
- </div>
-
- <script type="text/javascript">
-
- $(document).ready(
- function ()
- {
- $('#d1').datepicker({
- changeMonth: true,
- changeYear: true,
- dateFormat: 'dd/mm/yy'
- });
- $('#d2').datepicker({
- changeMonth: true,
- changeYear: true,
- dateFormat: 'dd/mm/yy'
- });
-
- showRegimen();
- }
- )
- function showRegimen() {
-
- $('#NVP').html(0);
- $('#EFV').html(0);
-
- $.ajax({
- URL: "/api/Home/ReplaceTLD?str=12",
- method: "POST",
- dataType: "Json",
- success: function (data) {
- console.log(data);
-
- if (data.errCode > 0) {
- $('#NVP').html(data.itemCount.nvp);
- $('#EFV').html(data.itemCount.efv);
- }
- else {
- console.log(data.errMsg);
- }
- },
- error: function (e) {
- console.log(e);
- }
- });
- }
-
- $('#btn-process').click(function () {
- correctRegimens();
- });
-
- //?FrmDates=' + str1 + '&ToDates=' + str2 + ''
- function correctRegimens() {
- var DDates = new Object();
- DDates.fromDate = $('#d1').val();
- DDates.toDate = $('#d2').val();
-
- var str1 = $('#d1').val();
- var str2 = $('#d2').val();
-
- if (str1 != null && str2 != null) {
-
- $('#btn-process').html('Please wait...');
- $('#btn-process').prop('disabled', false);
-
- var URLs = '/api/Home/correctRegimen';
-
- $.ajax({
- method: "POST",
- URL: URLs,
- dataType: "Json",
- data: JSON.stringify(DDates),
- success: function (data) {
- console.log(data);
-
- if (data != null && data.errCode > 0) {
- $('#btn-process').html('Process');
- $('#btn-process').prop('disabled', false);
- }
-
- },
- error: function () {
- alert("Error occured!!")
- }
-
- });
-
- }
-
- }
-
-
- </script>
-
-
-
-
-
- </div>
This the Model Class:- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
-
- namespace DRUG_REPLACEMENT.Models
- {
- public class LoadTLD
- {
- public class FromTo
- {
- public DateTime FrmDate { get; set; }
- public DateTime ToDate { get; set; }
- }
- public class ItemCount
- {
- public int EFV { get; set; }
- public int NVP { get; set; }
- }
- public class ErrMsg
- {
- public int errCode { get; set; }
- public string errMsg { get; set; }
- public ItemCount ItemCount { get; set; }
- public FromTo FromTo { get; set; }
- }
- }
- }
And this the Controller methods:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using System.Data;
- using MySql.Data.MySqlClient;
- using Newtonsoft.Json;
- using static DRUG_REPLACEMENT.Models.LoadTLD;
-
- namespace DRUG_REPLACEMENT.Controllers
- {
- public class HomeController : Controller
- {
-
- public int NVP1;
- public int EFV1;
-
-
-
-
- private readonly IConfiguration configuration;
- public HomeController(IConfiguration config)
- {
- this.configuration = config;
- }
-
- public ActionResult Index()
- {
- return View();
- }
- public ActionResult ReplaceTLD()
- {
- return View();
-
- }
-
- [HttpPost]
- public ActionResult<ErrMsg> ReplaceTLD(string str)
- {
- ErrMsg msg = new ErrMsg();
-
- try
- {
- var con = configuration.GetConnectionString("NMRSCon");
- using (MySqlConnection cn = new MySqlConnection(con))
- {
- cn.Open();
-
- var Sql = string.Concat("DROP TABLE IF EXISTS temp_regimen;",
- "CREATE TABLE temp_regimen AS ",
- "SELECT p.patient_id, pid.identifier, IF(obs.concept_id = 164506, cn1.name, NULL) AS regimen, obs.Value_coded,",
- "IF(obs.concept_id = 164506, obs.obs_datetime, NULL) AS Obs_Date FROM (",
- "(SELECT * FROM patient) AS p ",
- " LEFT JOIN ",
- " (SELECT * FROM patient_identifier) AS pid ON p.patient_id = pid.patient_id ",
- " LEFT JOIN ",
- " (SELECT * FROM Obs WHERE obs.concept_id = 164506 AND obs.value_coded = 164505 OR obs.Value_coded = 162565) AS obs ",
- " ON pid.patient_id = obs.person_id ",
- " LEFT JOIN ",
- " concept_name AS cn1 ON(obs.value_coded = cn1.concept_id AND cn1.locale = 'en' AND cn1.locale_preferred = 1) )");
-
-
- MySqlCommand cmd = new MySqlCommand(Sql, cn);
- cmd.ExecuteNonQuery();
- cmd.Dispose();
- cn.Close();
-
- if (cn.State == ConnectionState.Closed)
- cn.Open();
- var SQLNV = "SELECT COUNT(regimen) as regCount FROM temp_regimen WHERE regimen ='TDF-3TC-NVP'";
- MySqlCommand Ncmd = new MySqlCommand(SQLNV, cn);
- NVP1 = Convert.ToInt32(Ncmd.ExecuteScalar());
-
- var SQLEFV = "SELECT COUNT(*) from temp_regimen where regimen ='TDF-3TC-EFV'";
- MySqlCommand FVcmd = new MySqlCommand(SQLEFV, cn);
- EFV1 = Convert.ToInt32(FVcmd.ExecuteScalar());
-
- msg.ItemCount = new ItemCount
- {
- EFV = EFV1,
- NVP = NVP1
- };
- msg.errCode = 5;
- msg.errMsg = "Process completed successfully. Please check the output for feedback";
- return msg;
- }
- }
- catch (Exception e)
- {
- msg.errMsg = e.InnerException != null ? e.InnerException.Message : e.Message;
- msg.errCode = -1;
- return msg;
- }
-
- }
-
- [HttpPost]
- public ActionResult<ErrMsg> correctRegimen(string FrmDates, string ToDates)
- {
- ErrMsg msg = new ErrMsg();
- try
- {
- msg.FromTo = new FromTo
- {
- FrmDate = Convert.ToDateTime(FrmDates.Trim()),
- ToDate = Convert.ToDateTime(ToDates.Trim())
- };
-
- var USQL = string.Concat("UPDATE obs SET obs.Value_coded = 165681 where obs.Value_coded = 164505 OR ",
- "obs.Value_coded = 162565 AND TIMESTAMP(obs.obs_datetime) BETWEEN TIMESTAMP('" + msg.FromTo.FrmDate + "') ",
- "AND TIMESTAMP('" + msg.FromTo.ToDate + "') ");
- msg.errMsg = USQL;
- msg.errCode = 5;
- return msg;
- }
- catch (Exception e)
- {
- msg.errMsg = e.InnerException != null ? e.InnerException.Message : e.Message;
- msg.errCode = -1;
- return msg;
- }
-
- }
-
- }
- }
I need any suggestions and solution.
Thank you all