asp.net-mvc Razor 视图引擎:表达式树可能不包含动态操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4155392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Razor View Engine : An expression tree may not contain a dynamic operation
提问by femseks
I have a model similar to this:
我有一个类似的模型:
public class SampleModel
{
public Product Product { get; set; }
}
And in my controller I get an exception trying to print out
在我的控制器中,我尝试打印出异常
@Html.TextBoxFor(p => p.Product.Name)
This is the error:
这是错误:
Exception: An expression tree may not contain a dynamic operation
If anyone can give me some clues on how to fix this I would really appreciate it!
如果有人能给我一些关于如何解决这个问题的线索,我将不胜感激!
回答by marcind
It seems to me that you have an untyped view. By default, Razor views in MVC3 RC are typed as dynamic
. However, lambdas do not support dynamic members. You have to strongly type your model. At the top of your view file add
在我看来,您有一个无类型视图。默认情况下,MVC3 RC 中的 Razor 视图类型为dynamic
. 但是,lambda 不支持动态成员。你必须强类型你的模型。在视图文件的顶部添加
@model SampleModel
回答by felbus
A common error that is the cause of this is when you add
导致这种情况的一个常见错误是当您添加
@Model SampleModel
at the top of the page instead of
在页面顶部而不是
@model SampleModel
回答by Charlestown
In this linkexplain about @model, see a excerpt:
在此链接中解释@model,请参阅摘录:
@model
(lowercase "m") is a reserved keyword in Razor views to declare the model type at the top of your view. You have put the namespace too, e.g.:@model MyNamespace.Models.MyModel
Later in the file, you can reference the attribute you want with
@Model.Attribute
(uppercase "M").
@model
(小写“m”)是 Razor 视图中的保留关键字,用于在视图顶部声明模型类型。您也已经放置了命名空间,例如:@model MyNamespace.Models.MyModel
在文件的后面,您可以引用所需的属性
@Model.Attribute
(大写“M”)。
回答by Esteban Araya
Seems like your view is typed dynamic
. Set the right type on the view and you'll see the error go away.
似乎您的视图已输入dynamic
。在视图上设置正确的类型,您将看到错误消失。
回答by Kuber
Before using (strongly type html helper into view) this line
在使用之前(强烈在视图中输入 html helper)这一行
@Html.TextBoxFor(p => p.Product.Name)
You should include your model into you page for making strongly type view.
您应该将模型包含在页面中以制作强类型视图。
@model SampleModel
回答by JosephDoggie
This error happened to me because I had @@model instead of @model... copy & paste error in my case. Changing to @model fixed it for me.
这个错误发生在我身上,因为我有 @@model 而不是 @model... 在我的情况下复制和粘贴错误。更改为@model 为我修复了它。
回答by Perry
On vb.net you must write @ModelType.
在 vb.net 上你必须写@ModelType。