I work on asp.net mvc i face issue i can't auto update reason using java script ajax
auto update meaning when user start writing on reason it will save on database table
update i need will done without using update button it will auto save using java script ajax or jquery
my html as below
TypeScript
@model HR.WorkforceRequisition.Models.ResignationRequester
@Html.LabelFor(model => model.RequestNo, htmlAttributes: new { @class = "control-label col-md-5" })
@Model.RequestNo
@Html.TextAreaFor(model => model.Reason, new { id = "txtreason", @class = "form-control" })action will update on database will be as below
C#
public JsonResult Edit(ResignationRequester req)
{
dynamic responseData = new ExpandoObject();
responseData.success = false;
responseData.message = "";
string query;
ResignationRequester workforceRequest = ResignationupdateReason((int)req.RequestNo,req.Reason);
return Json(responseData);
}and javascript function will make update will be
JavaScript
java script function
function changereasonbasedonrequestno() {
console.log("fire event")
var ResignationRequester = new Object();
ResignationRequester.RequestNo = document.getElementById("RequestNo").innerHTML.trim();
ResignationRequester.NoticePeriod = document.getElementById("Reason").innerHTML.trim();
}
Amit MohantyRank #112 · 20K reputationPosted Dec 27, 2023, 12:38 AM EST
Check the example:
function autoUpdateReason() { var requestNo = document.getElementById("RequestNo").innerText.trim(); var reason = document.getElementById("txtreason").value.trim(); // Create an object to send data to the server var requestData = { RequestNo: requestNo, Reason: reason }; $.ajax({ type: "POST", url: "/YourController/Edit", // Replace with your actual controller URL data: JSON.stringify(requestData), contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { console.log("Reason updated successfully!"); }, error: function (error) { console.error("Error updating reason:", error); } }); } // Bind the autoUpdateReason function to the 'input' event of the textarea document.getElementById("txtreason").addEventListener("input", autoUpdateReason);