error Status: 'Internal Server Error'. Err

2019-08-19 06:57发布

I am using jqgrid Modal form to Add and Edit rows of the table. I am using Entity framework and Database First approach to create models from existing database. When I Add or Edit the rows, rows are added or edited correctly and being saved appropriately in the database and when I retrieve the added or edited rows and view in jqGrid, they are displayed correctly. But, the only issue is when I Add or Edit, it throws error Status: 'Internal Server Error'. Error Code: 500 in the modal form.

Anyone has any idea of why is this happening? Since, the add or edit is happening properly, is there a way that I can ignore this error message and make it not displayed at all?

Any help is greatly appreciated!

EDIT:

public ActionResult Create(string oper, string id, Employee employee)
{
try
{
 if (oper == "add")
 {
   if (ModelState.IsValid)
   {
    db.Employees.Add(employee);
    db.SaveChanges();
    return RedirectToAction("Index");   
   }
 }
 if (oper == "edit")
 {
  if (ModelState.IsValid)
  {
    db.Entry(employee).State = EntityState.Modified;
    db.SaveChanges();
    return RedirectToAction("Index");
  }
 }
  if (oper == "del") 
  {
    Employee employees = db.Employees.Find(Int32.Parse(id));
    db.Employees.Remove(employees);
    db.SaveChanges();
    return RedirectToAction("Index");
  }
}
catch (InvalidOperationException ex)
{
}
catch (Exception e)
{
}
}

1条回答
forever°为你锁心
2楼-- · 2019-08-19 07:16

Thanks to everyone for all your suggestions. I figured out what I was doing wrong. Actually, the issue is not with incorrect data format or the code not being hit, the problem was after I saved changes to my database, that's why it was not caught in the exception block.

After saving the changes to the database as [db.SaveChanges()], I am redirecting to the Index Action. That's where the issue was. I was redirecting to the wrong Index action. The code inside this Index action tried to return some value that is null and hence ArgumentNullException was thrown and that displayed YSOD.

Note: I was able to spot the ArgumentNullException with Event Viewer. Type Event Viewer in Run and the Goto Windows Logs - Application. You will be able to see all the Warnings for your application. Open the warning, Goto General tab and you will be able to see what kind of exception is thrown. Hope this helps someone in need.

查看更多
登录 后发表回答