Robert Koritnik
Robert Koritnik

Reputation: 105029

Visual Studio doesn't show Linq extensions in my Asp.net MVC views

This baffles me and I can't seem to make Visual Studio 2010 to recognise System.Linq extension methods in view code. Intellisense doesn't work and Visual Studio red underlines unrecognised extension methods.

These are web.config's most relevant parts, that I think are related to Visual Studio to recognize System.Linq extension methods. Commented out lines may be uncommented but there's no difference.

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

I have a partial view definition that looks like this. The same about commented out two lines. Uncommented make no difference:

<%@ 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 });
    }
%>

In this particular partial view Any() extension method is not recognised even though it's defined in System.Core assembly under System.Linq namespace.

Which config settings am I missing? Seems that Visual Studio can't see System.Core assembly to enumerate it's extension methods in System.Linq namespace.

Upvotes: 3

Views: 2714

Answers (3)

Robert Koritnik
Robert Koritnik

Reputation: 105029

Of course System.Core assembly has to be included in the configuration compilation.

But when I was cleaning up my web.config file I deleted this part, that is imperative (obviously) to Visual Studio 2010 to make things work as expected.

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

Upvotes: 1

amurra
amurra

Reputation: 15401

You need to add the attribute of targetFramework to the compilation element in your web.config:

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

Add that to your web.config and you should regain intellisense again in your views.

Upvotes: 5

Manatherin
Manatherin

Reputation: 4187

could try

<%@ Import Namespace="*" %>

where * is the namespace you want to use e.g. System.Linq

probably not the best way to do but it may work (not at a computer with vs so cant test)

Upvotes: 1

Related Questions