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

前端 未结 6 1813
孤街浪徒
孤街浪徒 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

    hmm, you can access ViewBag via ViewData, e.g. ViewContext.ViewData["Title"].

    So if you set ViewBag data in an action filter for example, you can pull it out from _ViewStart.cshtml using ViewContext.ViewData["Title"].

    But I tried to assign a value using ViewContext.ViewData["Key"] = value; and it doesn't seem to persist to the actual view.

提交回复
热议问题