I assume that you want this for the current request, and not for all requests, so the accepted answer is not correct. To exchange data with views or child controllers as part of a single request, the easiest way is to put your data in HttpContext.Items. This is used by all views and child controllers during the same request.
HttpContext.Items["UIOptions"] = new UIOptions { ShowMenu = true };
You can abstract this with the extension:
public static class HttpContextExtensions { public static UIOptions GetUIOptions(this HttpContext httpContext) { var options = httpContext.Items["UIOptions"] ?? (object) new UIOptions(); httpContext.Items["UIOptions"] = options; return options; } }
Then in your controller set the parameters
HttpContext.GetUIOptions().ShowMenu= true
In your view, refer to it as follows:
ViewContext.HttpContext.GetUIOptions()
I usually describe this further so that you customize it with attributes like
[UIOptions(ShowMenu=true)] public ActionResult MyAction() { return View(); }
So, you write an ActionFilter that checks the attributes of the action and sets the properties of the httpContext.GetUIOptions () object using the attribute properties during the ActionExecuting phase.
source share