情况是这样的:
我找不到获得的方法viewModel
已传递给 POST 操作方法。
[HttpPost]
public ActionResult Edit(SomeCoolModel viewModel)
{
// Some Exception happens here during the action execution...
}
在可重写的内部OnException
对于控制器:
protected override void OnException(ExceptionContext filterContext)
{
...
filterContext.Result = new ViewResult
{
ViewName = filterContext.RouteData.Values["action"].ToString(),
TempData = filterContext.Controller.TempData,
ViewData = filterContext.Controller.ViewData
};
}
调试代码时filterContext.Controller.ViewData
is null
因为代码执行时发生异常并且没有返回视图。
无论如何我看到了filterContext.Controller.ViewData.ModelState
已填满并具有我需要的所有值,但我没有完整的值ViewData => viewModel
对象可用。 :(
我想返回相同的View
与发布的data/ViewModel
返回给用户一个中心点。希望你明白我的意思。
我还有其他途径可以实现目标吗?
您可以创建一个继承自的自定义模型绑定器默认模型绑定器并将模型分配给TempData
:
public class MyCustomerBinder : DefaultModelBinder
{
protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
base.OnModelUpdated(controllerContext, bindingContext);
controllerContext.Controller.TempData["model"] = bindingContext.Model;
}
}
并将其注册到Global.asax
:
ModelBinders.Binders.DefaultBinder = new MyCustomerBinder();
然后访问它:
protected override void OnException(ExceptionContext filterContext)
{
var model = filterContext.Controller.TempData["model"];
...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)