I have an ActionFilter overridden by the OnActionExecuted method. The Context.Controller.ViewData.Model filter is always null for a POST operation. I found the following article, which seems to say that it should not be null, but it must have been an earlier version of MVC. This is MVC3. What should i get?
Model presence inside ActionFilter
UPDATE:
I understood the answer to the original question. I had a custom ActionResult that outputs JSON with custom date formatting. The problem was that the model is not installed in the controller.
In my custom ActionResult, the ExecuteResult method gets the passed ControllerContext, which would be nice if I could set the model there:
context.Controller.ViewData.Model = _data;
But this is already at the end of the loop, and the result is still not valid in ActionFilter. This means that I need to manually install the model in the controller:
ControllerContext.Controller.ViewData.Model = model;
Or
View(model);
Which means that I need to keep this in mind every time I use this custom ActionResult. Is there a more elegant way?
GIVE ANOTHER UPDATE:
I found a way to do this, it's just not as elegant as I had hoped.
In my constructor for comstom ActionResult, which I send to the controller, this method will at least always be consistent:
public JsonNetResult(object data, Controller controller) { SerializerSettings = new JsonSerializerSettings(); _data = data; controller.ControllerContext.Controller.ViewData.Model = _data; }
source share