提问人:LEVI A 提问时间:11/9/2023 最后编辑:marc_sLEVI A 更新时间:11/9/2023 访问量:60
数据不更新,仅在 EmployeeID 不等于 0 时添加记录
Data Not Updating, Only Adding Records When EmployeeID is Not Equal to 0
问:
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 });
}
}
}
这是jQuery:
$('#listTable').on('click', '.edit-button', function () {
var employeeApplicationId = $(this).data('id');
window.location.href = '/Employee/EmployeeEarnings?employeeId=' + employeeApplicationId;
});
我在代码中遇到了一个问题,即当 不等于 0 时,正在添加数据而不是更新数据。我已经彻底检查了我的代码,但找不到问题。EmployeeID
任何人都可以提供一些关于可能导致此行为的原因以及如何解决的指导或建议?提前致谢!
答:
0赞
Yat Fei Leong
11/9/2023
#1
最好检查员工 Employee.Id 在数据库中的位置。
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
}
评论
0赞
LEVI A
11/9/2023
它仍然不起作用
0赞
Yat Fei Leong
11/10/2023
你能发表你的观点吗?那一定是你的观点有问题。
0赞
Ashish Tripathi
11/9/2023
#2
请尝试以下代码:
看来你不见了
dbContext.Entry(existingEmployee) 中。状态 = 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;
}
评论