skip to Main Content
public class EmployeeController : Controller
{
    public ActionResult EmployeeEarnings(int? employeeId)  //GET
    {
        if (employeeId.HasValue)
        {
            using (var dbContext = new EmployeeDbContext())
            {
                var employee = dbContext.EmployeeApplications.Find(employeeId);

                if (employee != null)
                {
                    return View(employee);
                }
            }
        }

        return View(new EmployeeApplication());
    }

    [HttpPost]
    public ActionResult EmployeeEarnings(List<EmployeeApplication> employees)
    {
        try
        {
            if (employees != null && employees.Count > 0)
            {
                using (var dbContext = new EmployeeDbContext())
                {
                    foreach (var employee in employees)
                    {
                        if (!ModelState.IsValid)
                        {
                            var errors = ModelState.Values.SelectMany(v => v.Errors)
                                .Select(e => e.ErrorMessage).ToList();

                            ModelState.Clear();

                            return Json(new { success = false, message = "Validation errors", errors = errors });
                        }

                        if (employee.Id != 0)
                        {
                            var existingEmployee = dbContext.EmployeeApplications.Find(employee.Id);

                            if (existingEmployee != null)
                            {
                                existingEmployee.EmpCode = employee.EmpCode;
                                existingEmployee.EmpName = employee.EmpName;
                                existingEmployee.January = employee.January;
                                existingEmployee.February = employee.February;
                                existingEmployee.March = employee.March;
                                existingEmployee.April = employee.April;
                                existingEmployee.May = employee.May;
                                existingEmployee.June = employee.June;
                            }
                            else
                            {
                                return Json(new { success = false, message = "Employee not found" });
                            }
                        }
                        else
                        {
                            dbContext.EmployeeApplications.Add(employee);
                        }
                    }

                    dbContext.SaveChanges();

                   return Json(new { success = true, message = "Data saved" });
                }
            }
            else
            {
                return Json(new { success = false, message = "No data" });
            }
        }
        catch (Exception ex)
        {
            return Json(new { success = false, message = "Error occurred: " + ex.Message });
        }
    }
}

This is jQuery:

$('#listTable').on('click', '.edit-button', function () {
    var employeeApplicationId = $(this).data('id');
    window.location.href = '/Employee/EmployeeEarnings?employeeId=' + employeeApplicationId;
});

I’m encountering an issue in my code where data is being added instead of updated when the EmployeeID is not equal to 0. I’ve checked my code thoroughly and can’t find the issue.

Can anyone provide some guidance or suggestions on what might be causing this behavior and how to fix it? Thanks in advance!

2

Answers


  1. It is better to check where the Employee.Id in your employee even exist in the database.

    if (dbContext.EmployeeApplications.Where(x => x.Id == employee.Id).Any())
    {
    var existingEmployee = dbContext.EmployeeApplications.Find(employee.Id);
    
    existingEmployee.EmpCode = employee.EmpCode;
                                    existingEmployee.EmpName = employee.EmpName;
                                    existingEmployee.January = employee.January;
                                    existingEmployee.February = employee.February;
                                    existingEmployee.March = employee.March;
                                    existingEmployee.April = employee.April;
                                    existingEmployee.May = employee.May;
                                    existingEmployee.June = employee.June;
    
     dbContext.Update(existingEmployee);
    }
    else
    {
    // your code of adding employee
    }
    
    Login or Signup to reply.
  2. Plz try the below code:

    Seems you’re missing

    dbContext.Entry(existingEmployee).State = EntityState.Modified;

    if (existingEmployee != null)
    {
        existingEmployee.EmpCode = employee.EmpCode;
        existingEmployee.EmpName = employee.EmpName;
        existingEmployee.January = employee.January;
        existingEmployee.February = employee.February;
        existingEmployee.March = employee.March;
        existingEmployee.April = employee.April;
        existingEmployee.May = employee.May;
        existingEmployee.June = employee.June;
    
        dbContext.Entry(existingEmployee).State = EntityState.Modified;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search