0

My question is similar to this question: access database info in a partial view, .ascx that is included in Site.Master in asp.net mvc

My Site.Master is not bound to a View Model or a Controller. How can I gain access to User Context inside of a partial view which is loaded in my Site.Master file?

Community
  • 1
  • 1
  • Can you explain it more ? What exactly is being tried to achieve ? –  Mar 27 '13 at 16:54
  • 1
    I suspect this is an XY problem (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Tell us what you are trying to do with this code, in other words the real problem you are trying to solve. – Erik Funkenbusch Mar 27 '13 at 17:12
  • I have a partial view where I need the user's asp.net id to get other information about the user. Normally, if I am in a Controller GET method I can just call AppUserInstance.Id. But I am not in a controller so I can not do that. Thank you for your help. – user2211833 Mar 27 '13 at 17:58
  • I was able to go into my Controller base class and add it to the ViewData – user2211833 Mar 27 '13 at 19:18

1 Answers1

0

You can possible use Action Filters for this purpose. In Action filters you need to override OnActionExecuting method and pass the UserContext in filterContext.Controller.ViewBag.UserContext

This dynamic viewbag you can use in your master files anywhere.

Does this answer your question?

Edit:

You need a class inheriting from ActionFilterAttribute you can then ovverride its OnActionExecuting method like below:

public override void OnActionExecuting(ActionExecutingContext filterContext)
      {           
         //Your implementation here
         base.OnActionExecuting(filterContext);
      }
Aman
  • 1,292
  • 1
  • 14
  • 20