I am currently creating an ASP.NET MVC application. I am trying to add elements to the ViewBag property for all pages of my site. To do this, I created a basic controller, to which all my site controllers are inherited.
To add to the ViewBag, I redefined the OnActionExecuting method: protected override void OnActionExecuting(ActionExecutingContext filterContext)
I know that OnActionExecuting methods for MVC5 are not async, so I ran into this problem. I need to be able to call some form of the following code to retrieve the latest elements and put them in a ViewBag:
IList<PlaceListItemViewModel> places = await GetLatestPlaces(3); ViewBag.FooterLatestPlaces = places;
Using GetLatestPlaces(3).Result just causes a dead end, so this is not an option.
Can anyone give me an opportunity on how to achieve this. It also should not override the OnActionExecuting method, if there is another way to add elements to the ViewBag for each page (except for calling the code from each separate action on each controller), I am open to using this method.
Unfortunately, the GetLatestPlaces method cannot be asynchronous because it uses the MongoDB 2.0 driver, which is completely asynchronous.
source share