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!

1 Answer 1

11

You can denote multiple lines of code by wrapping it within a @{ code } for multi-line statements:

@{ 
    var test = new MyNamespace.AnotherViewModel();
    test.SomeAction();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.