Razor 视图引擎:表达式树可能不包含动态操作

问题描述:

我有一个类似的模型:

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)

这是错误:

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!

在我看来,您有一个无类型视图.默认情况下,MVC3 RC 中的 Razor 视图类型为 dynamic.但是,lambda 不支持动态成员.你必须强类型你的模型.在视图文件的顶部添加

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

@model SampleModel