how to override a views Layout declaration

前端 未结 4 1294
無奈伤痛
無奈伤痛 2021-01-19 15:43

In asp.net MVC 3 is there a way to override the Layout declaration set in a view from a controller or action filter?

@{
    Layout = \"~/Views/Shared/_Layout.cshtm         


        
4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-19 16:18

    Use ViewBag when you need to change the layout call an action and put the new layout (even null) in viewbag.

    @{
       Layout = ViewBag.layout;
    }
    

    and inside the action

    if(something)
       ViewBag.layout = "~/Views/Shared/whatever.cshtml";
    else
       ViewBag.layout = null;
    

提交回复
热议问题