Hello Team, Please am trying to pass data from viewmodel to the database and am getting this error like object undefined from the local host and no error on the console.
CONTROLLER CODE
public ActionResult SaveStaff(StaffManagementViewModel model)
{
ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
//string result = "Error Staff Info is not saved!";
var result = false;
try
{
if (model.CompanyId == null || model.CompanyId <= 0)
{
tblCompany comp = new tblCompany();
comp.DateJoin = model.DateJoin;
comp.Department = model.Department;
comp.JobTitle = model.JobTitle;
db.tblCompanies.Add(comp);
db.SaveChanges();
}
tblStaff staff = new tblStaff();
staff.StaffNo = model.StaffNo;
staff.FName = model.FName;
staff.LName = model.LName;
staff.BirthDate = model.BirthDate;
staff.PhoneNo = model.PhoneNo;
staff.Email = model.Email;
staff.FirstName = model.FirstName;
staff.LastName = model.LastName;
staff.PhoneNumb = model.PhoneNumb;
staff.CompanyId = model.CompanyId;
db.tblStaffs.Add(staff);
db.SaveChanges();
result = true;
}
catch (Exception ex)
{
throw ex;
}
return Json(result, JsonRequestBehavior.AllowGet);
}
JavaScript
function saveStaffProfile() {
debugger;
var tblStaffs = [];
var comp = new Object();
comp.CompanyId = $("#companyId").val();
comp.DateJoin = $("#date").val();
comp.Department = $("#department").val();
comp.JobTitle = $("#jobTilte").val();
var staff = new Object();
staff.StaffId = $("#staffId").val();
staff.StaffId = $("#image").val();
staff.StaffNo = $("#staffNo").val();
staff.FName = $("#fName").val();
staff.LName = $("#lName").val();
staff.BirthDate = $("#birthDate").val();
staff.PhoneNo = $("#phone").val();
staff.Email = $("#email").val();
staff.FirstName = $("#firstName").val();
staff.LastName = $("#lastName").val();
staff.PhoneNumb = $("#phonenumber").val();
staff.tblCompany = comp;
return $.ajax({
contentType: 'application/json; charset=ut-8',
dataType: 'json',
type: 'POST',
url: "/Home/SaveStaff",
data: JSON.stringify({
model: staff
}),
success: function (result) {
if (result.status = "saved") {
//GetAllCategory();
//Reset();
//dataTable.ajax.reload();
$("#StaffModal").modal('hide');
} else {
swal("Save Failed!")
}
},
error: function () {
swal("Error!")
}
});
}
Vishal JoshiPosted May 30, 2023, 5:53 AM
Hello Emmmanuel,
It looks like you miss to assign a new company id to the model company id. please check out the below code.
Thanks
Vishal
Emmmanuel FIADUFEPosted Jun 1, 2023, 10:22 AM
Thank you Vishal
Mohamed Azarudeen ZPosted May 24, 2023, 1:50 PM
sure emmanuel happy to help
Emmmanuel FIADUFEPosted May 24, 2023, 12:54 PM
Thank you Mohamed, I will check and revert please
Mohamed Azarudeen ZPosted May 24, 2023, 12:49 PM
here are a few possible solutions you can try:
Check the URL: Ensure that the URL in your JavaScript code,
url: "/Home/SaveStaff", corresponds to the correct controller and action in your ASP.NET MVC application. Make sure that the controller name and action name are accurate and properly spelled.Verify the routing configuration: Verify that you have set up the appropriate routing configuration in your ASP.NET MVC application's
RouteConfig.csfile or in your routing configuration if you're using attribute routing. Ensure that the route for theSaveStaffaction is correctly defined.Check the parameter name: In your JavaScript code, you're sending the data as
model: staff. Make sure that the parameter name in your controller action,SaveStaff(StaffManagementViewModel model), matches the name used in the JavaScript code (model). Double-check that the property names in yourStaffManagementViewModelclass match the ones used in the JavaScript code as well.Verify the data format: Make sure that the data being sent in the request payload is correctly formatted as JSON. In your JavaScript code, you have
contentType: 'application/json; charset=ut-8', which seems to have a typo. It should becontentType: 'application/json; charset=utf-8'(note the correct spelling ofutf-8). Additionally, ensure that theStaffManagementViewModelclass in your server-side code accurately represents the structure of the JSON object being sent from the client-side code.