2011-03-25 87 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