在剃刀视图Asp.net Mvc 4中实例化一个对象(Instantiate an object in razor view Asp.net Mvc 4)
我有一个强烈绑定到viewmodel的剃刀视图:
@model MyNamespace.MyViewModel我想在同一个视图页面中创建另一个viewmodel的实例并使用它:
@test = new MyNamespace.AnotherViewModel(); @test.SomeAction();我收到编译错误:
The name 'test' does not exist in the current context我是asp.net mvc的新手并且无法使其工作。 任何帮助将不胜感激。 谢谢!
I have a razor view strongly bound to a viewmodel:
@model MyNamespace.MyViewModelI 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 contextI am very new to asp.net mvc and was not able to make it work. Any help will be appreciated. Thanks!
最满意答案
您可以通过将其包含在用于多行语句的@{ code }来表示多行代码:
@{ var test = new MyNamespace.AnotherViewModel(); test.SomeAction(); }You can denote multiple lines of code by wrapping it within a @{ code } for multi-line statements:
@{ var test = new MyNamespace.AnotherViewModel(); test.SomeAction(); }更多推荐
发布评论