Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiate an object in razor view Asp.net Mvc 4

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!

like image 701
Mdb Avatar asked Nov 06 '12 08:11

Mdb


1 Answers

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

@{ 
    var test = new MyNamespace.AnotherViewModel();
    test.SomeAction();
}
like image 97
Paolo Moretti Avatar answered Oct 05 '22 12:10

Paolo Moretti