Adalat  Khan

Adalat Khan

  • 626
  • 1.5k
  • 846.1k

Date Retrieving Problem

Feb 25 2019 12:36 AM
I am retrieving data from database in MVC core using jquery Ajax. All the fields have successfully retrieved and work well but the datae fields cannot be retrieved. For date i used the date control i.e. <input type="date" />. Please check only the following fields:
  1. $("#dtpEmpBirthDate").val(response[i].EmpBirthDate);  
  2. $("#dtpEmpJoiningDate").val(response[i].EmpJoiningDate);  
Please explain how to add the retrieved date value from the database into the date control. The above two date fields do not populate with the response object. Following is my complete code of jquery Ajax:
  1. //Retrieve the Employee record using the selected Employee ID  
  2. $.ajax({  
  3. url: "/RetrieveData/RetrieveEmployeeRecordByID",  
  4. type: "GET",  
  5. dataType: "json",  
  6. data: { "id": empID },  
  7. success: function (response) {  
  8. for (var i = 0; i < response.length; i++) {  
  9. $("#txtEmployeeCode").val(response[i].EmpCode);  
  10. $("#txtEmpFirstName").val(response[i].EmpFirstName);  
  11. $("#txtEmpLastName").val(response[i].EmpLastName);  
  12. $("#txtEmpFullName").val(response[i].EmpFullName);  
  13. $("#dtpEmpBirthDate").val(response[i].EmpBirthDate);  
  14. $("#cmbEmpGender").val(response[i].EmpGender);  
  15. $("#txtEmpFullAddress").val(response[i].EmpFullAddress);  
  16. $("#dtpEmpJoiningDate").val(response[i].EmpJoiningDate);  
  17. $("#cmbEmpReligion").val(response[i].EmpReligion);  
  18. $("#txtEmpTelephoneNumber").val(response[i].EmpTelephoneNumber);  
  19. $("#txtEmpMobileNumber").val(response[i].EmpMobileNumber);  
  20. $("#txtEmpEmailAddress").val(response[i].EmpEmailAddress);  
  21. $("#cmbEmpBranchCode").val(response[i].EmpBranchCode);  
  22. $("#cmbEmpDepttCode").val(response[i].EmpDepttCode);  
  23. $("#cmbEmpOccupationCode").val(response[i].EmpOccupationCode);  
  24. $("#cmbEmpNationality").val(response[i].EmpNationality);  
  25. $("#cmbEmpMaritalStatus").val(response[i].EmpMaritalStatus);  
  26. $("#txtEmpDependent").val(response[i].EmpDependent);  
  27. $("#txtEmpEmergencyContact").val(response[i].EmpEmergencyContact);  
  28. $("#txtEmpEmergencyContactPerson").val(response[i].EmpEmergencyContactPerson);  
  29. $("#txtEmpEmergencyContactPersonRelation").val(response[i].EmpEmergencyContactPersonRelation);  
  30. $("#txtEmployeePictureFullPath").val(response[i].EmpPicture);  
  31. //End of for Loop  
  32. },  
  33. error: function (response) {  
  34. alert("Record cannot be displayed", response.responseText);  
  35. }  
  36. });  
The above Jquery Ajax successfully retrieves data from the database and all the fields are populated well but only the date fields cannot be populated. Please give me solution.
 
Following is the HTML code of the date fields:
  1. <div class="col-md-3">  
  2. <div class="form-group">  
  3. <label asp-for="EmpBirthDate" class="control-label label-size">Birth Date</label>  
  4. <input type="date" asp-for="EmpBirthDate" id="dtpEmpBirthDate" class="form-control textbox" />  
  5. </div>  
  6. </div>  
  7. <div class="col-md-3">  
  8. <div class="form-group">  
  9. <label asp-for="EmpJoiningDate" class="control-label label-size">Joining Date</label>  
  10. <input type="date" asp-for="EmpJoiningDate" id="dtpEmpJoiningDate" class="form-control textbox" />  
  11. </div>  
  12. </div>  
Following is the code of my Action Method:
  1. public JsonResult RetrieveEmployeeRecordByID(string id)  
  2. {  
  3. var sa = new JsonSerializerSettings();  
  4. var empRecord = from rec in dbContext.EmployeeGeneralDetails  
  5. where rec.EmpCode == id  
  6. select rec;  
  7. return Json(empRecord, sa);  
  8. }  

Answers (5)