2

這讓我感到困惑,我似乎無法讓Visual Studio 2010識別視圖代碼中的System.Linq擴展方法。 Intellisense不起作用,Visual Studio紅色強調了無法識別的擴展方法。Visual Studio在我的Asp.net MVC視圖中不顯示Linq擴展

這些都是web.config最相關的部分,我認爲它們與Visual Studio有關,以識別System.Linq擴展方法。註釋掉的行可能沒有註釋,但沒有區別。

<compilation debug="true" batch="true"> 
    <assemblies> 
     <!-- 
     <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> 
     <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> 
     --> 
     <add assembly="System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
     <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
     <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
    </assemblies> 
</compilation> 

<pages enableViewState="false"> 
    <namespaces> 
     <add namespace="System.Collections.Generic"/> 
     <add namespace="System.Linq"/> 
     <add namespace="System.Web.Mvc"/> 
     <add namespace="System.Web.Mvc.Html"/> 
     <add namespace="System.Web.Routing"/> 
     <add namespace="MyApp.Objects"/> 
     <add namespace="MyApp.Web.General"/> 
     <add namespace="MyApp.Web.Helpers"/> 
    </namespaces> 
</pages> 

我有一個看起來像這樣的局部視圖定義。同樣的評論了兩行。取消註釋沒有什麼區別:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IList<ToolbarItem>>" %> 
<%-- 
<%@ Assembly Name="System.Core" %> 
<%@ Import Namespace="System.Linq" %> 
--%> 
<% 
    if (!this.Model.Any(ti => ti is ToolbarText && (ti as ToolbarText).MaximizeWidth)) 
    { 
     this.Model.Add(new ToolbarText { MaximizeWidth = true }); 
    } 
%> 

在這個特殊的局部視圖Any()擴展方法無法識別,即使它在System.Core大會的定義下System.Linq命名空間。

我缺少哪些配置設置?似乎Visual Studio無法看到System.Core程序集來枚舉它的System.Linq命名空間中的擴展方法。

+0

這是什麼類型?模型? – 2010-11-11 10:51:06

+0

@傑克遜:開玩笑?您可以在<%@ Control%>定義中看到...它是'IList '類型的。 – 2010-11-11 10:53:06

+0

不是開玩笑 - 不適用於ASP.Net-MVC/ASP.Net。 – 2010-11-11 10:57:08

回答

1

當然System.Core程序集必須包含在配置編譯中。

但是,當我清理我的web.config文件時,我刪除了這個部分,這對Visual Studio 2010來說是必不可少的(顯然)使事情按預期工作。

<system.codedom> 
    <compilers> 
     <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4"> 
      <providerOption name="CompilerVersion" value="v3.5"/> 
      <providerOption name="WarnAsError" value="false"/> 
     </compiler> 
    </compilers> 
</system.codedom> 
1

可以嘗試

<%@ Import Namespace="*" %> 

其中*爲要使用例如命名空間System.Linq的

可能不是最好的辦法,但它可能工作(而不是用電腦VS所以不能測試)

+0

不...已經嘗試過了...還有'<@ Assembly Name =「System.Core」%>'...沒有區別。我會更新我的問題,指出我也嘗試過。 – 2010-11-11 11:06:35

5

您需要targetFramework的屬性添加到編輯元素在你的web.config :

<system.web> 
    <compilation debug="true" targetFramework="4.0"> 

將其添加到您的web.config中,您應該在視圖中重新獲得智能感知。

+0

添加此屬性使編輯器中的事情變得很好。但事情是我的目標是3.5。所以我得到運行時錯誤,該屬性無法識別。 – 2010-11-11 18:18:03

相關問題