I am looking to store data in a database from my MVC ASP.net project without the need to refresh the view page. My approach involves creating a string, sending it to the controller, and then pushing it to the database. I have already developed a function for string creation...
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);
}
An example of the resulting string is as follows:
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
To accomplish this task, I would like to know how to 1.) Transfer this string to the controller and 2.) Utilize the controller to integrate this into my database?
All the data points align with the table "RecordEntry" in my database and have been named accordingly.
EDIT: I have incorporated this function in the view now. However, when executed, it does not proceed beyond the alert(chain) statement. Do you have any insights into why this may be happening? The other commented-out alert does respond.
EDIT: It just dawned on me that there was no AJAX call within the function but I had placed it outside. Is it essential to include it within the function so that it gets invoked upon pressing a submit button?
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 the submit (next) button used:
<a class="btn btn-success btn-block btn-lg " onclick="NextButtonClick()" id="nextbutton"><font color="white">Next</font></a>
Furthermore, here is the function called by the button which in turn triggers multiple other functions:
function NextButtonClick() {
HideSelectionsBeforeNextSelection();
ShowDropDownsAgain();
//removefromList();
ResetValues();
IncreaseRecordIdByOne();
CreateChain();
}