Global Error handling using PartialView in MVC

冷暖自知 提交于 2019-12-18 09:46:31

问题


I have look at one of the perfect article on Exception handling in ASP.NET MVC and want to implement a method as on Method 6 in this article in order to reuse the same error page for all of other modal dialogs on error and exception situations. On the other hand, as I use popup window, I need to render a PartialView in my modal dialog instead of redirecting the page. Is it possible to do this?

AJAX call:

$.ajax({
    type: "POST",
    url: '@Url.Action("Delete", "Person")',
    cache: false,
    dataType: "json",
    data: formdata,

    success: function (response, textStatus, XMLHttpRequest) {
        if (response.success) {
            //display operation result
        }
        else {
            /* At this stage I need to render the Error view as partial without 
            redirecting to Error page on error. The problem at here not directly 
            related to rendering partialview. I need to render the global Error 
            page and reuse it for every error occured on my modal dialog */
            @Html.Partial("~/Views/Home/Error.cshtml", Model)
        }
    }
});

Controller:

[HttpPost]
public JsonResult Delete(int id)
{
    try
    {
        //code omitted for brevity
    }
    catch (Exception  ex)
    {
        return Json(new { success = false, message = "Operation failed." });
    }
}

来源:https://stackoverflow.com/questions/39594409/global-error-handling-using-partialview-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!