您的位置:首页 > 其它

如何在MVC3 razor视图下的ViewsStart文件中设置使用两套不同的Layout布局视图

2012-08-16 14:30 701 查看
http://stackoverflow.com/questions/5161380/how-do-i-specify-different-layouts-in-the-asp-net-mvc-3-razor-viewstart-file

问题描述: 如何在同一应用程序中应用两套布局视图,两个不同控制器对应的使用两套不同的视图,如PublicControlller对应_PublicLayout而StuffController对应_StuffLayout,

如何通过在_ViewStart文件中进行相关设置使得View/Public文件夹下的文件默认使用的_PublicLayout布局页,而View/Stuff文件夹下的文件默认使用的_StuffLayout布局页。

解决方案:

1)可以在各自文件夹中放置新的_ViewStart.cshtml文件改写View文件夹下的_ViewStart并制定使用特定的_Layout布局页。如View/Public和View/stuff文件夹下各自有

自己的_ViewStart文件设置布局页面。

2)在controller下的action方法中指定使用某个布局页面,如

returnView("Index","~/Views/Shared/_StaffLayout.cshtml", someViewModel);

3)自定义一个AcitonFilter类在调用执行action时来改写使用的布局页

publicclassLayoutInjecterAttribute:ActionFilterAttribute
{
privatereadonlystring _masterName;
publicLayoutInjecterAttribute(string masterName)
{
_masterName = masterName;
}

publicoverridevoidOnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
var result = filterContext.ResultasViewResult;
if(result !=null)
{
result.MasterName= _masterName;
}
}
}

通过在controller下的action方法添加特性来指定选择某特定布局页

[LayoutInjecter("_PublicLayout")]
publicActionResultIndex()
{
returnView();}

}


http://forums.asp.net/t/1642656.aspx/1


问题描述:在_ViewStart文件中使用ViewBag对象

解决方案:(可通过此方法根据特定参数来切换布局页实现多语言网站设计)

设置:ViewContext.Controller.ViewBag.StoreName = "My New Store";

使用:@ViewContext.Controller.ViewBag.StoreName

Generally, ViewData["StoreName"] is same as ViewBag.StoreName

Also, Controller.ViewData["StoreName"] = Controller.StoreName = ViewContext.Controller.ViewBag.StoreName =ViewContext.Controller.ViewData["StoreName"]

But every view and partial view gets its own instance of viewdata.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐