在剃刀视图Asp.net Mvc 4中实例化一个对象(Instantiate an object in razor view Asp.net Mvc 4)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
在剃刀视图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.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!

最满意答案

您可以通过将其包含在用于多行语句的@{ 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(); }

更多推荐

本文发布于:2023-04-12 20:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/65ef7ad1f4fedd3720a6c5e7e9d172cd.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:剃刀   视图   实例   对象   Asp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!