5
Answers

Object not define

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);
}
C#

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!")
        }
    });
}
JavaScript
Answers (5)
2
Vishal Joshi

Vishal Joshi

247 7.8k 134.7k 1y

Hello Emmmanuel,

It looks like you miss to assign a new company id to the model company id. please check out the below 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();
             model.CompanyId = comp.CompanyId;
         }
         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);
}

Thanks

Vishal

Accepted
2
Emmmanuel FIADUFE

Emmmanuel FIADUFE

684 1.4k 70.7k 1y

Thank you Mohamed, I will check and revert please 

1
Emmmanuel FIADUFE

Emmmanuel FIADUFE

684 1.4k 70.7k 1y

Thank you Vishal 

1
Mohamed Azarudeen Z

Mohamed Azarudeen Z

135 13.8k 193.9k 1y

sure emmanuel happy to help

1
Mohamed Azarudeen Z

Mohamed Azarudeen Z

135 13.8k 193.9k 1y

here are a few possible solutions you can try:

  1. 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.

  2. Verify the routing configuration: Verify that you have set up the appropriate routing configuration in your ASP.NET MVC application's RouteConfig.cs file or in your routing configuration if you're using attribute routing. Ensure that the route for the SaveStaff action is correctly defined.

  3. 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 your StaffManagementViewModel class match the ones used in the JavaScript code as well.

  4. 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 be contentType: 'application/json; charset=utf-8' (note the correct spelling of utf-8). Additionally, ensure that the StaffManagementViewModel class in your server-side code accurately represents the structure of the JSON object being sent from the client-side code.