I am attempting to encapsulate data forms into an object as shown in the image below...
https://i.sstatic.net/PK60S.pngHere is how I have structured my object in MVC:
Employee
public class Employee
{
public int Position { get; set; }
public int Salary { get; set; }
public Biodata? Biodata { get; set; }
public EmergencyContactPerson? EmergencyContactPerson { get; set; }
}
Biodata
public class Biodata
{
public string Fullname { get; set; }
public int Gender { get; set; }
public string Email { get; set; }
}
EmergecnyContactPerson
public class EmergencyContactPerson
{
public string Fullname { get; set; }
public int Connection { get; set; }
public string NumberPhone { get; set; }
}
After successfully wrapping the data, encountering issues when trying to send it to the controller. Below is my Ajax syntax:
$("#pageEmployee-input").ready(function () {
$("#btnSave").click(function () {
var biodata = {}, employee = {}, emergencyContactPerson = {};
var test = $("#formBiodata").serialize()
$("#formBiodata").serializeArray().map(function (x) { biodata[x.name] = x.value; });
$("#formEmployee").serializeArray().map(function (x) { employee[x.name] = x.value; });
$("#formEmergencyContactPerson").serializeArray().map(function (x) { emergencyContactPerson[x.name] = x.value; });
var dataFormObject = {};
dataFormObject.Position = employee.Position
dataFormObject.Salary = employee.Salary
dataFormObject.Biodata = biodata
dataFormObject.EmergencyContactPerson = emergencyContactPerson
var dataForm = JSON.stringify(dataFormObject)
debugger
$.ajax({
url: "Home/Create",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: dataForm,
success: function (data) {
console.log(data);
},
error: function (data) {
alert(data.responseText);
}
});
})
})
When debugging in the controller, the following issue arises... https://i.sstatic.net/1rj66.png
Help needed with fixing syntax or procedure.