我正在开发移动Web应用程序.我需要让当前的displaymode在控制器中移动.
我的问题是:我有2个部分视图
/Views/Shared/ListItem.cshtml
/Views/Shared/ListItem.mobile.cshtml
Run Code Online (Sandbox Code Playgroud)
当使用PartialView("ListItem")这是正确的工作.但我需要将partialviews放在子文件夹中
/Views/Shared/Modules/Post/ListItem.cshtml
/Views/Shared/Modules/Post/ListItem.mobile.cshtml
Run Code Online (Sandbox Code Playgroud)
当我使用PartialView("〜/ Views/Shared/Modules/Post/ListItem.cshtml")时,这适用于桌面.当displaymode是移动时,ListItem.mobile.cshtml
不显示.
我的选择是
if( CurrentDisplayMode==Mobile){
PartialView("~/Views/Shared/Modules/Post/ListItem.mobile.cshtml");
else
PartialView("~/Views/Shared/Modules/Post/ListItem.cshtml");
Run Code Online (Sandbox Code Playgroud)
怎么弄CurrentDisplayMode
?如何解决这个问题呢?
我还需要访问当前的显示模式,这样我就可以调整传递给视图的视图模型(移动视图中的信息较少,因此可以从较小的视图模型中显示).
ControllerContext.DisplayMode
无法使用,因为它将在执行操作后设置.
所以你必须根据上下文(用户代理,cookie,屏幕大小等)确定显示模式.
这是我在ASP.NEt论坛上发现的一个很好的技巧,它可以让你使用框架后来使用的相同逻辑来确定显示模式:
public string GetDisplayModeId()
{
foreach (var mode in DisplayModeProvider.Instance.Modes)
if (mode.CanHandleContext(HttpContext))
return mode.DisplayModeId;
throw new Exception("No display mode could be found for the current context.");
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3469 次 |
最近记录: |