Creating an Object in Asp.net Mvc 4 Razor Mode

I have a razor view strongly attached to viewmodel:

@model MyNamespace.MyViewModel 

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

 @test = new MyNamespace.AnotherViewModel(); @test.SomeAction(); 

I get a compilation error:

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

I am very new to asp.net mvc and could not get it to work. Any help would be appreciated. Thank you

+4
source share
1 answer

You can mark multiple lines of code by enclosing it in @{ code } for multi-line statements:

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

Source: https://habr.com/ru/post/1444285/


All Articles