Why can't _ViewStart.cshtml access the ViewBag object?

前端 未结 6 1735
孤街浪徒
孤街浪徒 2020-12-30 19:12

I have the default _ViewStart.cshtml in my /Views folder. I\'d like to be able to access my ViewBag object so I can set the default title for all my views.

However,

6条回答
  •  借酒劲吻你
    2020-12-30 19:54

    You can achieve this using Partial views. Put all your Title related common code in a Partial View called Title.cshtml in the shared folder. In the _viewstart simply call the Partial view.

    _ViewStart.cshtml:

    @{
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    @Html.Partial("Title")
    

    ~/Shared/Title.cshtml:

    @{
     ViewBag.Title = "bytecourse - Online Courses in Technology"; 
    }
    

提交回复
热议问题