Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception: An expression tree may not contain a dynamic operation

Tags:

c#

razor

.net-4.0

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?

like image 577
Kyle Avatar asked Apr 24 '12 15:04

Kyle


2 Answers

Your View seems incorrect.

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

@model Project.Data.Organization
like image 130
Joshua Avatar answered Nov 06 '22 08:11

Joshua


The fix is

@model ProjectName.Models.Modelname

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

like image 36
Sunny Okoro Awa Avatar answered Nov 06 '22 06:11

Sunny Okoro Awa