首页 > Web开发 > 详细

MVC3 Razor 根据不同页面使用不同Layout

时间:2014-01-14 19:47:44      阅读:451      评论:0      收藏:0      [点我收藏+]

_ViewStart.cshtml运行于每一Page前, 所以通常在这里先设置Layout

 
下面代码为特定的controller指定Index,Edit,Create的模板
即Index对应  _Index_Layout, Edit对应_Index_Layout...
 
@{
    var controller =  ViewContext.RouteData.Values["controller"].ToString().ToLower();
    var action = ViewContext.RouteData.Values["action"].ToString().ToLower();
 
    string[] lController = new string[] { "user", "news", "mail" };
    string[] lAction = new string[] { "index", "edit", "create" };
         
    if (lController.Contains(controller)){
        if (lAction.Contains(action))
        {
            Layout = "~/Views/Shared/_" + action  + "_Layout.cshtml";
        }else if (action == ""){
            Layout = "~/Views/Shared/_Index_Layout.cshtml";
        }
    }
     
}

MVC3 Razor 根据不同页面使用不同Layout

原文:http://www.cnblogs.com/zcm123/p/3512868.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!