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
C#
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
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 JoshiRank #377 · 6.7K reputationPosted May 30, 2023, 1:53 AM EDT
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
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted May 24, 2023, 8:54 AM EDT
Thank you Mohamed, I will check and revert please
Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted May 24, 2023, 8:49 AM EDT
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.Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted May 24, 2023, 9:50 AM EDT
sure emmanuel happy to help
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Jun 1, 2023, 6:22 AM EDT
Thank you Vishal