I need to save data to a database from my MVC ASP.net project without refreshing the view page. To do this I plan to create a string and send it to the controller to then send to the database. I have already made a function to create the string...
function CreateChain() {
RecordID = document.getElementById("RecordIdTextBox").value
RecordDate = document.getElementById("RecordDateEntry").value
Employee = document.getElementById("EmployeeTextBox").value
Department = document.getElementById("ddlDepartmentsOption").value
SubDepartment = document.getElementById("ddlSubDepartments").value
Machine = document.getElementById("ddlMachines").value
Equipment = document.getElementById("ddlEquipment").value
Problem = document.getElementById("InvisibleProblemsddl").value
Grade = document.getElementById("Gradeddl").value
GradeComment = document.getElementById("GradeComment").value
WorkOrderStatus = document.getElementById("WorkOrderStatus").value
WorkOrderNumber = document.getElementById("WorkOrderNumber").value
chain = ("RecordID:" + RecordID + ',' +
"RecordDate:"+ RecordDate + ',' +
"Employee:"+ Employee + ',' +
"DepartmentID:"+ Department + ',' +
"SubDepartmentID:"+ SubDepartment + ',' +
"MachineID:"+ Machine + ',' +
"EquipmentID:"+ Equipment + ',' +
"ProblemID:"+ Problem + ',' +
"Grade:"+ Grade + ',' +
"GradeComment:"+ GradeComment + ',' +
"WorkOrderStatus:"+ WorkOrderStatus + ',' +
"WorkOrderNumber:"+ WorkOrderNumber)
console.log(chain);
}
And an example of the string looks like this
RecordID:5,RecordDate:2021-08-02T13:50:46,Employee:Josh,DepartmentID:1,SubDepartmentID:1,MachineID:16,EquipmentID:141,ProblemID:59,Grade:A,GradeComment:the machine is working,WorkOrderStatus:true,WorkOrderNumber:123456
How can I 1.) Send this string to the controller and 2.) Use the controller to send this to my database?
All data points correspond to table RecordEntry in my database and have the same name as given.
EDIT: This is my function in view now. When ran it does not get down to the alert(chain). Do you know why? The other alert that is commented out does hit.
EDIT: I realized that you did not have the ajax within the function and I did there. Should it be in the function so it is called on a submit button press?
function CreateChain() {
//alert("running function createchain");
Record = document.getElementById("RecordIdTextBox").value
RecordDate = document.getElementById("RecordDateEntry").value
Employee = document.getElementById("EmployeeTextBox").value
Department = document.getElementById("ddlDepartmentsOption").value
SubDepartment = document.getElementById("ddlSubDepartments").value
Machine = document.getElementById("ddlMachines").value
Equipment = document.getElementById("ddlEquipment").value
Problem = document.getElementById("InvisibleProblemsddl").value
Grade = document.getElementById("Gradeddl").value
GradeComment = document.getElementById("GradeComment").value
WorkOrderStatus = document.getElementById("WorkOrderStatus").value
WorkOrderNumber = document.getElementById("WorkOrderNumber").value
return {
"RecordID": Record,
"RecordDate": RecordDate,
"Employee": Employee,
"DepartmentID": Department,
"SubDepartmentID": SubDepartment,
"MachineID": Machine,
"EquipmentID": Equipment,
"ProblemID": Problem,
"Grade": Grade,
"GradeComment": GradeComment,
"WorkOrderStatus": WorkOrderStatus,
"WorkOrderNumber": WorkOrderNumber
};
var chain = CreateChain();
alert(chain);
$.ajax({
url: "/RecordEntries/AddtoDatabase",
type: "POST",
data: { model: chain },
success: function (result) {
alert("Success");
},
error: function (xhr, exception) {
alert("Error");
}
});
}
This is my submit (next) button
<a class="btn btn-success btn-block btn-lg " onclick="NextButtonClick()" id="nextbutton"><font color="white">Next</font></a>
And the function that is called that calls multiple other functions.
function NextButtonClick() {
HideSelectionsBeforeNextSelection();
ShowDropDownsAgain();
//removefromList();
ResetValues();
IncreaseRecordIdByOne();
CreateChain();
}