Kyle
Kyle

Reputation: 17677

Exception: An expression tree may not contain a dynamic operation

I found a similar question here: Razor View Engine : An expression tree may not contain a dynamic operation

But I have tried the solutions and they do not solve my issue. I have a Controller with the following:

    public ActionResult CreateOrganization(Guid parentOrganizationId)
    {
        Organization organization = new Organization();

        return View(organization);
    }

And the view has the following:

@using Project.Data
@Model Project.Data.Organization

@{
    ViewBag.Title = "Create new Organization";
}

<hgroup class="title">
    <h1>@ViewBag.Title.</h1>
    <h2>Use the form below to create a new Organization.</h2>
</hgroup>

<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

@using (Html.BeginForm((string)ViewBag.FormAction, "Organization"))
{
    <fieldset>
        <legend>Create Organization</legend>
        <ol>
            <li>
                @Html.LabelFor(m=> m.Name)
                @Html.TextBoxFor(m=> m.Name);
                @Html.ValidationMessageFor(m=> m.Name)
            </li>
        </ol>
        <input type="submit" value="Register" />
    </fieldset>
}

Which is explicitly specifying the model (as suggested in the other post). However I am still receiving the 'An expression tree may not contain a dynamic operation' error. Have I made a stupid mistake somewhere?

Upvotes: 5

Views: 8070

Answers (3)

Sunny Okoro Awa
Sunny Okoro Awa

Reputation: 531

The fix is

@model ProjectName.Models.Modelname

Models >> if use model class in in model folder ProjectName.MVC Model path

Upvotes: 1

Kenneth Ito
Kenneth Ito

Reputation: 5261

Are you using dynamic types anywhere?

This looks very similar to your problem. Error: An expression tree may not contain a dynamic operation

You could use a similar work around to the one in the link (casting).

Upvotes: 0

Joshua
Joshua

Reputation: 4139

Your View seems incorrect.

The model declaration should be lowercase 'model', not 'Model':

@model Project.Data.Organization

Upvotes: 9

Related Questions