如何创建一个ViewBag一个BaseController [英] How to create a BaseController with a ViewBag

查看:336
本文介绍了如何创建一个ViewBag一个BaseController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做到以下几点:
我有一些现成控制器运行,但现在我想创建一个 BaseController
我的每控制器应该继承它是这样的:

I need to do the following: I have some Controllers ready and running, but now I want to create a BaseController. Each of my Controllers should inherit from it like this:

public class MySecondController : BaseController

这就是到目前为止已经运行。 现在的问题:

我要添加 ViewBag 进入这个基地控制器。这 ViewBag 应该从每一个被称为我的看法控制器accessable。

I want to add a ViewBag into this base controller. This ViewBag should be accessable from every view which is called in my controllers.

如何实现这一点?

推荐答案

覆盖OnActionExecuting方法,添加whetever要添加到ViewBag

override OnActionExecuting method, add whetever you want to add to ViewBag

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

这篇关于如何创建一个ViewBag一个BaseController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆