我正在使用ASP.NET MVC 4(RC)视图的自定义DisplayModes来允许自定义视图内容。为什么ViewContext.DisplayMode.DisplayModeId为空?
我给这家移动唯一的应用程序规则非常简单,定义了基于用户的内部轮廓/会话配置:
DisplayModeProvider.Instance.Modes.Insert(0,
new DefaultDisplayMode("Tablet")
{
ContextCondition = (context =>
{
if (SessionManager.Current == null)
return false; // assume phone initially
if (SessionManager.Current.Session.ScreenWidth > 600)
return true;
return false;
})
});
DisplayModeProvider.Instance.Modes.Insert(0,
new DefaultDisplayMode("Phone")
{
ContextCondition = (context =>
{
if (SessionManager.Current == null ||
SessionManager.Current.Session.ScreenWidth <= 600)
return true; // assume phone initially
return false;
})
});
我现在可以创建使用xxxx.Phone.cshtml和xxxx.Tablet意见。 CSHTML。呈现正确的视图。
但是,ViewContext.DisplayMode.DisplayModeId总是回到空白。例如,在下面的页面我回显的显示模式和硬编码的ID,告诉其观点被渲染:
<div>@Model.Distance - @ViewContext.DisplayMode.DisplayModeId - Tablet</div>
我看到硬编码的平板电脑(或手机)的值,但DisplayModeId始终是空白。
应该设置这个值吗?我认为这在以前的MVC 4测试版中有效,但我不是100%确定的。
任何想法?
做了一些更多的测试。因此无法确认其与最新的MVC4版本。工作是否像预期的那样。我可以确认的是,第一个带ContextCondition = null的DisplayMode被返回,独立于其他。 – EricSch