1

我正在开发一个 MVC 5 互联网应用程序,并且有一个关于将 an 传递object给 shared的问题view

我在共享文件夹中有一个view调用。CustomError.cshtmlview具有以下模型类型:@model CanFindLocation.ViewModels.CustomErrorViewModel

如何从 a 中的函数将类型的对象传递CanFindLocation.ViewModels.CustomErrorViewModel给 this ?viewprotected override void OnException(ExceptionContext filterContext)controller

这是我的代码:

protected override void OnException(ExceptionContext filterContext)
{
    Exception e = filterContext.Exception;

    if (e is HttpRequestValidationException)
    {
        filterContext.ExceptionHandled = false;
        customErrorViewModel = customErrorService.GetDefaultCustomError(customErrorType, "Test message.");
        RedirectToAction("CustomError", customErrorViewModel);
    }
}

没有显示,而是view调用了以下函数:

protected void Application_Error(object sender, EventArgs e)

提前致谢。

4

1 回答 1

0

我不认为您可以根据需要返回视图,因此我通常将值放入 TempData 并重定向到主页或任何登录页面。

主页检查此 Viewbag 中是否有值,如果有错误则显示错误。

控制器:

public class BaseController : Controller
    {
        protected void SetError(string message, params object[] args)
        {
            TempData["UIError"] = string.Format(message, args);
        }
    }

在我的共享(主)布局视图中:

@if (TempData["UIError"] != null)
        {
            <div class="alert alert-danger" role="alert">
                <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                <span class="sr-only">Error:</span>
                @TempData["UIError"]
            </div>
        }
于 2014-12-08T11:46:46.263 回答