【问题标题】:ASP.NET MVC 3 - An expression tree may not contain a dynamic operationASP.NET MVC 3 - 表达式树可能不包含动态操作
【发布时间】:2012-09-07 12:39:15
【问题描述】:

我得到:表达式树可能不包含动态操作

当我这样做时:

@Html.TextBoxFor(m => m.Translations[ViewBag.Languages[i].CultureCode].Title)
<br/>
@Html.TextBoxFor(m => m.Translations[ViewBag.Languages[i].CultureCode].PreviewDescription)
<br/>
@Html.TextBoxFor(m => m.Translations[ViewBag.Languages[i].CultureCode].FullDescription)

我之前在做更简单的TextBoxFor,例如:

@Html.TextBoxFor(m => m.ContactName)
@Html.TextBoxFor(m => m.EmailAddress)

在视图的顶部,我的 using 语句类似于:@model x.y.z.Listing

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    正如消息所说,您在 lambda 表达式中使用了 viewbag,这是一个动态操作:

    @Html.TextBoxFor(m => m.Translations[ViewBag.Languages[i].CultureCode].Title)
    

    要解决您的问题,只需将动态移出,就像这样

    @{
        string cultureCode = ViewBag.Languages[i].CultureCode
    }
    
     @Html.TextBoxFor(m => m.Translations[cultureCode].Title) 
    

    【讨论】:

      【解决方案2】:

      我认为,您必须检查您使用的是强类型视图。

      【讨论】:

      • 这可能更适合作为评论,但它仍然是一个好点。
      猜你喜欢
      • 2012-05-13
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 2011-10-30
      • 2012-05-05
      • 1970-01-01
      • 2013-10-17
      相关资源
      最近更新 更多