且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET MVC5从布局访问ViewBag?

更新时间:2023-02-24 22:46:37

我建议您使用布局所使用的操作方法,该方法可以传递所需的数据.例如

I would suggest that you have an action method, which the layout uses, that passes the data you need. For example

public class ControllerName : Controller
{
    public ActionMethod GetData()
    {
       return Content("Some data"); // Of whatever you need to return.
    }
}

然后在布局和可以调用的视图中

then in the layout and the view you can call

@Html.Action("GetData", "ControllerName")