2011-03-25 67 views
1

我想知道如何在Razor中实现这一点。如何在Razor中针对枚举类型进行枚举值检查

目前,我有这里面一些JavaScript:

if (@Convert.ToInt16(ViewBag.Status.Type) == @Convert.ToInt16(StatusType.Info)) 
{ 
    // do something here 
} 

但我不知道是否有更好的办法?似乎有点cludgy的做法...

回答

2

这里有几个选择,我倾向于去与JavaScript渲染,除非if()匹配(这是一个未使用的代码块情况下,右),是这样的:

@if ((int)ViewBag.Status.Type == (int)StatusType.Info) 
{ 
    @:document.getElementById('test')... 
} 

或为大型块,<text>例如:

@if ((int)ViewBag.Status.Type == (int)StatusType.Info) 
{ 
    <text> 
    var elem = document.getElementById('test'); 
    elem.focus(); 
    </text> 
} 
+0

为伟大的感谢! – jaffa 2011-03-25 15:26:53