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?
OnException()
method you could try something likeMyModel m = new MyModel(); TryUpdateModel<MyModel>(m);
to give you the model, but of course you would need to know the type so not very flexible – BosniaModelState
values but the bad part is what you mentioned: it's not dynamic\flexible. I'm looking to add some bit ofdynamic
to this. :) If I get this to work it will allow me to avoid writing a lot of repetitive code. – Baneful