【问题标题】:Return viewBag into Master page MVC Razor将 viewBag 返回到主页面 MVC Razor
【发布时间】:2013-11-15 22:48:06
【问题描述】:

我想将ViewBag 返回到我的母版页,但我对 MVC Razor 没有太多经验,所以我不知道如何解决我的问题,这是我的代码:

控制器:

MasterController.cs

public class SharedController : Controller
{
    public ActionResult SiteMaster()
    {
        ViewBag.PageTitle = "Show this text into my MasterPage";
        return View();
    }
}

HomeController.cs

    public class HomeController : Controller
    {
      public ActionResult Index()
      {
        return View();
      }
    }

观看次数:

共享 -> SiteMaster.cshtml

<html>
 <head></head>
 <body>
 <div>@ViewBag.PageTitle</div>  //Doesn't work
 <div>@RenderBody()</div>
 </body>
</html>

首页 -> Index.cshtml

@{
  Layout = "~/Views/Shared/SiteMaster.cshtml";
 } 
 <div>Test</div>

App_Start -> RouteConfig.cs

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

【问题讨论】:

  • 您希望每次显示视图时都显示ViewBag.PageTitle 吗?
  • 是的,因为我有母版页,所以我需要在包含 Master Page 的每个页面上显示 View Bag

标签: c# asp.net-mvc-4 razor controller master-pages


【解决方案1】:

您可以使用全局 ActionFilter 来做到这一点:

public class MasterPageTitleFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        filterContext.Controller.ViewBag.PageTitle = "Show this text into my MasterPage";
        base.OnActionExecuted(filterContext);
    }
}

如果您将此过滤器设置为一个操作,它将在它结束时触发,在生成视图之前,以便在ViewBag 中添加您的标题。

为了在不标记所有动作/控制器的情况下将其用于每个动作/控制器,请将其添加到您的全局过滤器注册中:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    // other filters to add
    filters.Add(new MasterPageTitleFilterAttribute());
}

【讨论】:

    【解决方案2】:

    我相信你只需要将你的 viewbag 分配到你的 homecontroller 的 index 动作中。未调用您的站点管理员操作。

    【讨论】:

    • 我刚刚尝试过,但它不起作用,但是,我需要该 viewbag 仅显示在母版页布局上,并与其他页面分开
    • 样本有代码有时会覆盖 cshtml 文件中的 viewbag 项目 - 也许检查它没有在你的剃须刀中以某种方式重置?
    • 当您将 viewbag 分配放在您的 index 操作中并尝试在您的 index.cshtml 中显示它时会发生什么 - 它至少能做到那么远吗?仅供参考 - 我尝试了你在一个新项目中描述的内容,它工作正常(即在 INDEX 操作中分配)。
    【解决方案3】:

    您可以创建一个包含重写方法的基本控制器

    public class BaseController: Controller
    {
        public override void OnActionExecuting(ActionExecutedContext filterContext)
            {
                filterContext.Controller.ViewBag.PageTitle = "Show this text into my MasterPage";
                //base.OnActionExecuted(filterContext);
            }
    }
    

    那么您将从 BaseController 继承您的其他控制器。结果,您每次都会在 masterPage 中到达 ViewBag.PageTitle..

    public class HomeController : BaseController
    {
    ...
    }
    

    【讨论】:

    • 错误 1 ​​'Belina.Controllers.BaseController.OnActionExecuted(System.Web.Mvc.ActionExecutedContext)': 覆盖 'protected' 继承成员 'System.Web.Mvc.Controller.OnActionExecuted 时无法更改访问修饰符(System.Web.Mvc.ActionExecutedContext)'
    • 好吧,你是对的,我试过了,我错过了我编辑的答案,不是 OnActionExecuted 它必须是 OnActionExecuting 才有效。
    【解决方案4】:

    您需要在操作中分配您的 viewbag。

    public class HomeController : Controller
    {
          public ActionResult Index()
          {
            ViewBag.PageTitle = "Show this text into my MasterPage";
            return View();
          }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-19
      • 2020-08-11
      • 2019-04-27
      • 2018-10-11
      • 1970-01-01
      • 2018-05-06
      • 2022-11-01
      • 2017-05-04
      • 2017-06-08
      相关资源
      最近更新 更多