Emmmanuel FIADUFE

Emmmanuel FIADUFE

  • 789
  • 1.1k
  • 54k

Object not define

May 24 2023 12:44 PM

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!")
        }
    });
}

 


Answers (5)