【问题标题】:Exception: An expression tree may not contain a dynamic operation例外:表达式树可能不包含动态操作
【发布时间】:2012-05-05 05:58:34
【问题描述】:

我在这里发现了一个类似的问题: Razor View Engine : An expression tree may not contain a dynamic operation

但是我已经尝试了解决方案,但它们并没有解决我的问题。我有一个具有以下功能的控制器:

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

        return View(organization);
    }

视图有以下内容:

@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>
}

这是明确指定模型(如另一篇文章中所建议)。但是,我仍然收到“表达式树可能不包含动态操作”错误。我是不是在某个地方犯了一个愚蠢的错误?

【问题讨论】:

  • 您也可以发布您的组织 POCO 吗?可能它是动态的或具有动态属性?

标签: c# .net-4.0 razor


【解决方案1】:

您的视图似乎不正确。

模型声明应该是小写的'model',而不是'Model':

@model Project.Data.Organization

【讨论】:

    【解决方案2】:

    您是否在任何地方使用动态类型?

    这看起来与您的问题非常相似。 Error: An expression tree may not contain a dynamic operation

    您可以使用与链接中的类似的工作(投射)。

    【讨论】:

      【解决方案3】:

      解决办法是

      @model ProjectName.Models.Modelname
      

      Models >> 如果在模型文件夹中使用模型类 ProjectName.MVC 模型路径

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-13
        • 2013-07-27
        • 1970-01-01
        • 2011-10-30
        • 1970-01-01
        • 2012-09-07
        • 2011-05-08
        相关资源
        最近更新 更多