Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Viewbag property on all views

How can I access some ViewBag properties across all my views? I want to have some information like current user name, etc accessible everywhere, but without having to to specifically define the properties in each ActionResult method on my project

like image 887
Kenny D. Avatar asked Dec 05 '14 03:12

Kenny D.


People also ask

Can ViewData be accessed via the View Bag property?

To pass the strongly typed data from Controller to View using ViewBag, we have to make a model class then populate its properties with some data and then pass that data to ViewBag with the help of a property. And then in the View, we can access the data of model class by using ViewBag with the pre-defined property.

How do you call ViewBag in view?

This can be accessed in the view like @ViewBag.Name. You can assign a primitive or a complex type object as a value to ViewBag property. You can assign any number of properties and values to ViewBag.

Can we pass ViewBag from view to controller?

Yes you cannot pass a Viewbag from view to controller. But you can pass them using TempData.


3 Answers

The best and straight forward way to accomplish your requirement is to make a Custom Base Controller and inherit your Controller from this Base Controller.

public class MyBaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        ViewBag.someThing = "someThing"; //Add whatever
        base.OnActionExecuting(filterContext);
    }
}

Now instead of inheriting Controller class,inherit MyBaseController in your Controller as shown :-

public class MyOtherController : MyBaseController 
{
    public ActionResult MyOtherAction()
    {
       //Your Stuff
       return View();
    }
    //Other ActionResults
}
like image 68
Kartikeya Khosla Avatar answered Nov 15 '22 13:11

Kartikeya Khosla


You can achieve what you want in a number of ways, each one with their pros and cons.

1. With a Base Class

public class BaseController : Controller
{
    protected override ViewResult View(IView view, object model)
    {
        this.ViewBag.MyProperty = "value";
        return base.View(view, model);
    }
}

PROS: Quite simple to implement, few lines of code, highly reusable, can be opted-out at will (see comments below).

CONS: Being forced to derive all your controllers from a base class might have some impact, especially if you have a lot of controllers already in place and/or you need to derive them from other base classes.

2. With a Module

public class ViewBagPropertyModule: Module
{
    protected override void AttachToComponentRegistration(IComponentRegistry cr,
                                                  IComponentRegistration reg)
    {
        Type limitType = reg.Activator.LimitType;
        if (typeof(Controller).IsAssignableFrom(limitType))
        {
            registration.Activated += (s, e) =>
            {
                dynamic viewBag = ((Controller)e.Instance).ViewBag;
                viewBag.MyProperty= "value";
            };
        }
    }
}

PROS: None I’m aware of.

CONS: None I’m aware of (except being a bit counterintuitive).

3. With a RegisterController Hook

builder.RegisterControllers(asm)
    .OnActivated(e => {
        dynamic viewBag = ((Controller)e.Instance).ViewBag;
        viewBag.MyProperty = "value";
    });

PROS: Fast, secure, reusable: ideal for any IoC design pattern.

CONS: Not always suited for small project and/or simple websites: if you’re not using IoC you’re often not using RegisterController at all.

4. With an ActionFilter attribute

public class MyPropertyActionFilter : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.Controller.ViewBag.MyProperty = "value";
    }
}

and then in your Global.asax.cs file:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalFilters.Filters.Add(new MyPropertyActionFilter(), 0);
}

PROS: Easily the less-obtrusive method amongst those mentioned.

CONS: None I’m aware of.

I also wrote this article on my blog explaining all the above methods.

like image 22
Darkseal Avatar answered Nov 15 '22 13:11

Darkseal


One way: Create a custom attribute, then you can apply it globally in the FilterConfig. Then you don't have to do anything in your controllers.

public class MyCustomViewActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        dynamic ViewBag = filterContext.Controller.ViewBag;

        ViewBag.Id = "123";
        ViewBag.Name = "Bob";
    }
}

In App_Start/FilterConfig.cs:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new MyCustomViewActionFilter());
    }

Another way if all you need is the User information. You can add the following to the top of your view:

@using Microsoft.AspNet.Identity

Then access your User Name using the following syntax:

@User.Identity.GetUserName()

You can also override the IPrincipal implementation and provide your own properties and methods to add more information you need to render.

UPDATE: looking at MVC 6 in Asp.Net vNext this is actually baked into the framework. http://www.asp.net/vnext/overview/aspnet-vnext/vc#inj

like image 8
Ashley Lee Avatar answered Nov 15 '22 12:11

Ashley Lee