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

femseks picture femseks · Nov 11, 2010 · Viewed 105.5k times · Source

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!

Answer

marcind picture marcind · Nov 11, 2010

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