在.Net MVC3中如何处理JqGrid中的服务器端错误?

时间:2011-10-03 06:59:50

标签: asp.net-mvc-3 jqgrid

我正在使用JqGrid和MVC 3。

当我尝试删除行时,在服务器端代码中处理了一些错误。

如何将此错误消息传递给JqGrid?

例如,在操作方法中:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Delete(int id) {
    Project project = dbContext.Projects.Find(id);
    dbContext.Projects.Remove(project);

    try {
        dbContext.SaveChanges();
    } catch (DbUpdateException){
        // Send the error to JqGrid
    }

    return RedirectToAction("Index");
}

在JqGrid中:

$('#DataTable').        
jqGrid('navGrid', '#pager',
{ add: true, del: true, edit: true, search: false },
{ url: Url("Edit", controllerName), closeAfterEdit: true },
{ url: Url("Create", controllerName), closeAfterAdd: true },            
{ url: Url("Delete", controllerName) }
);

方法URL只是创建了操作方法的URL

2 个答案:

答案 0 :(得分:2)

我找到了解决方案。

只是从catch块抛出异常,JqGrid会捕获它!

虽然我没想到!!!

像:

        try {
            dbContext.SaveChanges();
        } catch (DbUpdateException){
            throw new Exception("Could not delete project.");
        }

答案 1 :(得分:1)

try
{
     dbContext.SaveChanges();
     return Json(true);
} 
catch (DbUpdateException)
{
     var json = new { msgkey = "Could not delete project." };
     return Json(json);
}