asp.net-mvc – 在内部OnException(ExceptionContext filterContext)中,有什么方法可以恢复传递给POST动作的模型?

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 在内部OnException(ExceptionContext filterContext)中,有什么方法可以恢复传递给POST动作的模型?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
情况是这样的:

我找不到获取传递给POST操作方法viewmodel的方法.

  1. [HttpPost]
  2. public ActionResult Edit(SomeCoolModel viewmodel)
  3. {
  4. // Some Exception happens here during the action execution...
  5. }

在控制器的可覆盖的OnException内:

  1. protected override void OnException(ExceptionContext filterContext)
  2. {
  3. ...
  4.  
  5. filterContext.Result = new ViewResult
  6. {
  7. ViewName = filterContext.RouteData.Values["action"].ToString(),TempData = filterContext.Controller.TempData,ViewData = filterContext.Controller.ViewData
  8. };
  9. }

调试代码时,filterContext.Controller.ViewData为null,因为代码执行时发生了异常,并且未返回任何视图.

无论如何,我看到filterContext.Controller.ViewData.ModelState被填充并具有我需要的所有值,但我没有完整的ViewData => viewmodel对象可用.

猜你在找的asp.Net相关文章