Razor View Engine : An expression tree may not contain a dynamic operation

北战南征 提交于 2019-12-17 06:07:17

问题


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!


回答1:


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



回答2:


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



回答3:


In this link explain about @model, see a excerpt:

@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").




回答4:


Seems like your view is typed dynamic. Set the right type on the view and you'll see the error go away.




回答5:


Before using (strongly type html helper into view) this line

@Html.TextBoxFor(p => p.Product.Name)

You should include your model into you page for making strongly type view.

@model SampleModel



回答6:


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.



来源:https://stackoverflow.com/questions/4155392/razor-view-engine-an-expression-tree-may-not-contain-a-dynamic-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!