2012-11-06 88 views
4

我有強烈綁定到一個視圖模型剃刀觀點:實例化一個對象4

@model MyNamespace.MyViewModel 

我不想再拍視圖模型的實例在同一視圖頁面,並使用它:

@test = new MyNamespace.AnotherViewModel(); 

@test.SomeAction(); 

我收到編譯錯誤:

The name 'test' does not exist in the current context 

我很新的asp.net的MVC,卻不能使它發揮作用。任何幫助將不勝感激。謝謝!

回答

8

您可以通過一個@{ code }內包裝是多行語句表示多行代碼:

@{ 
    var test = new MyNamespace.AnotherViewModel(); 
    test.SomeAction(); 
} 
+0

謝謝!這樣做的工作。 – Mdb