Any way to recover model passed to POST action when inside OnException(ExceptionContext filterContext)?

核能气质少年 提交于 2019-12-19 07:01:10

问题


Situation is this:

I can't find a way of getting the viewModel that was passed to the POST action method.

[HttpPost]
public ActionResult Edit(SomeCoolModel viewModel)
{
    // Some Exception happens here during the action execution...
}

Inside the overridable OnException for the controller:

protected override void OnException(ExceptionContext filterContext)
{
    ...

    filterContext.Result = new ViewResult
    {
        ViewName = filterContext.RouteData.Values["action"].ToString(),
        TempData = filterContext.Controller.TempData,
        ViewData = filterContext.Controller.ViewData
    };
}

When debugging the code filterContext.Controller.ViewData is null since the exception occurred while the code was executing and no view was returned.

Anyways I see that filterContext.Controller.ViewData.ModelState is filled and has all the values that I need but I don't have the full ViewData => viewModel object available. :(

I want to return the same View with the posted data/ViewModel back to the user in a central point. Hope you get my drift.

Is there any other path I can follow to achieve the objective?


回答1:


You could create a custom model binder that inherits from DefaultModelBinder and assign the model to TempData:

public class MyCustomerBinder : DefaultModelBinder
{
    protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        base.OnModelUpdated(controllerContext, bindingContext);

        controllerContext.Controller.TempData["model"] = bindingContext.Model;
    }
}

and register it in Global.asax:

ModelBinders.Binders.DefaultBinder = new MyCustomerBinder();

then access it:

protected override void OnException(ExceptionContext filterContext)
{
    var model = filterContext.Controller.TempData["model"];

    ...
}


来源:https://stackoverflow.com/questions/25213998/any-way-to-recover-model-passed-to-post-action-when-inside-onexceptionexception

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