5

I have a razor view strongly bound to a viewmodel:

@model MyNamespace.MyViewModel

I want to make an instance of another viewmodel in the same view page and use it:

@test = new MyNamespace.AnotherViewModel();

@test.SomeAction();

I receive compilation error:

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

I am very new to asp.net mvc and was not able to make it work. Any help will be appreciated. Thanks!

4

1 に答える 1

11

@{ code }複数行のステートメントの場合は、コードをforで囲むことにより、コードの複数行を示すことができます。

@{ 
    var test = new MyNamespace.AnotherViewModel();
    test.SomeAction();
}
于 2012-11-06T09:01:33.527 に答える