How to set ViewBag properties for all Views without using a base class for Controllers?

后端 未结 8 2063
故里飘歌
故里飘歌 2020-11-28 01:21

In the past I\'ve stuck common properties, such as the current user, onto ViewData/ViewBag in a global fashion by having all Controllers inherit from a common base controlle

相关标签:
8条回答
  • 2020-11-28 01:54

    I have found the following approach to be the most efficient and gives excellent control utilizing the _ViewStart.chtml file and conditional statements when necessary:

    _ViewStart:

    @{
     Layout = "~/Views/Shared/_Layout.cshtml";
    
     var CurrentView = ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString();
    
     if (CurrentView == "ViewA" || CurrentView == "ViewB" || CurrentView == "ViewC")
        {
          PageData["Profile"] = db.GetUserAccessProfile();
        }
    }
    

    ViewA:

    @{
       var UserProfile= PageData["Profile"] as List<string>;
     }
    

    Note:

    PageData will work perfectly in Views; however, in the case of a PartialView, it will need to be passed from the View to the child Partial.

    0 讨论(0)
  • 2020-11-28 01:55

    Un-tried by me, but you might look at registering your views and then setting the view data during the activation process.

    Because views are registered on-the-fly, the registration syntax doesn't help you with connecting to the Activated event, so you'd need to set it up in a Module:

    class SetViewBagItemsModule : Module
    {
        protected override void AttachToComponentRegistration(
            IComponentRegistration registration,
            IComponentRegistry registry)
        {
            if (typeof(WebViewPage).IsAssignableFrom(registration.Activator.LimitType))
            {
                registration.Activated += (s, e) => {
                    ((WebViewPage)e.Instance).ViewBag.Global = "global";
                };
            }
        }
    }
    

    This might be one of those "only tool's a hammer"-type suggestions from me; there may be simpler MVC-enabled ways to get at it.

    Edit: Alternate, less code approach - just attach to the Controller

    public class SetViewBagItemsModule: 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.Config = e.Context.Resolve<Config>();
                    viewBag.Identity = e.Context.Resolve<IIdentity>();
                };
            }
        }
    }
    

    Edit 2: Another approach that works directly from the controller registration code:

    builder.RegisterControllers(asm)
        .OnActivated(e => {
            dynamic viewBag = ((Controller)e.Instance).ViewBag;
            viewBag.Config = e.Context.Resolve<Config>();
            viewBag.Identity = e.Context.Resolve<IIdentity>();
        });
    
    0 讨论(0)
  • 2020-11-28 01:55

    You don't have to mess with actions or change the model, just use a base controller and cast the existing controller from the layout viewcontext.

    Create a base controller with the desired common data (title/page/location etc) and action initialization...

    public abstract class _BaseController:Controller {
        public Int32 MyCommonValue { get; private set; }
    
        protected override void OnActionExecuting(ActionExecutingContext filterContext) {
    
            MyCommonValue = 12345;
    
            base.OnActionExecuting(filterContext);
        }
    }
    

    Make sure every controller uses the base controller...

    public class UserController:_BaseController {...
    

    Cast the existing base controller from the view context in your _Layout.cshml page...

    @{
        var myController = (_BaseController)ViewContext.Controller;
    }
    

    Now you can refer to values in your base controller from your layout page.

    @myController.MyCommonValue
    
    0 讨论(0)
  • 2020-11-28 01:57

    If you want compile time checking and intellisense for the properties in your views then the ViewBag isn't the way to go.

    Consider a BaseViewModel class and have your other view models inherit from this class, eg:

    Base ViewModel

    public class BaseViewModel
    {
        public bool IsAdmin { get; set; }
    
        public BaseViewModel(IUserService userService)
        {
            IsAdmin = userService.IsAdmin;
        }
    }
    

    View specific ViewModel

    public class WidgetViewModel : BaseViewModel
    {
        public string WidgetName { get; set;}
    }
    

    Now view code can access the property directly in the view

    <p>Is Admin: @Model.IsAdmin</p>
    
    0 讨论(0)
  • 2020-11-28 02:01

    You could use a custom ActionResult:

    public class  GlobalView : ActionResult 
    {
        public override void ExecuteResult(ControllerContext context)
        {
            context.Controller.ViewData["Global"] = "global";
        }
    }
    

    Or even a ActionFilter:

    public class  GlobalView : ActionFilterAttribute 
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.Result = new ViewResult() {ViewData = new ViewDataDictionary()};
    
            base.OnActionExecuting(filterContext);
        }
    }
    

    Had an MVC 2 project open but both techniques still apply with minor changes.

    0 讨论(0)
  • 2020-11-28 02:03

    Brandon's post is right on the money. As a matter of fact, I would take this a step further and say that you should just add your common objects as properties of the base WebViewPage so you don't have to cast items from the ViewBag in every single View. I do my CurrentUser setup this way.

    0 讨论(0)
提交回复
热议问题