MVC3 Razor 根据不同页面使用不同Layout
时间:2014-01-14 19:47:44
收藏:0
阅读:454
_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";
}
}
}
原文:http://www.cnblogs.com/zcm123/p/3512868.html
评论(0)