Model Availability Inside ActionFilter

I created a new ActionFilter for the ASP.NET MVC application that I am creating. I have an action that accepts an Http message, and the action method argument takes an object for which I created and registered a custom mediator.

I noticed that inside IActionFilter.OnActionExecuting value of filterContext.Controller.ViewData.Model always null , although it looks like model binding is always called before the OnActionExecuting action OnActionExecuting . In contrast, inside the IActionFilter.OnActionExecuted method of the same action filter, the value of filterContext.Controller.ViewData.Model not null.

Do you guys know if this is by design or a mistake? If the design is their links that describe why this is so? Thanks.

0
source share
2 answers

Since the Controller action is responsible for creating the Model and passing it to the view, how could the model exist before the action was called? You do not know which model will be created until this happens.

+1
source

They say that you usually install a model during the execution of an action. Therefore, it is natural that the model is zero when OnActionExecuting is executed, which occurs before the action is called.

+1
source

Source: https://habr.com/ru/post/889229/


All Articles