Search code examples
asp.net-mvcrazorviewusing

How to enable the effect of "using" for a view?


The page I'm developing's had the syntax as follows.

@model List<Group>
@{ Layout = "~/Views/Shared/_Layout.cshtml"; }

All of the sudden, because of a change that I'm not aware of (and can't retrace), VS starts demanding that I specify a full qualifier like this.

@model System.Collections.Generic.List<Group>
@{ Layout = "~/Views/Shared/_Layout.cshtml"; }

When I do that, Resharper marks it as necessary (which it should be, according to the code from before). My guess is that I've unknowingly removed a using somewhere but I can't figure of where.

Since I'm using Razor and MVC, there's no code-behind and frankly speaking I wonder where the information about that the List came from before it broke. I mean, there's got to be an information about the full qualifier System.Collections.Generic somewhere. Where is it supposed to be?


Solution

  • The framework will use namespaces defined in the Web.config found in the Views folder (not the application Web.config). The syntax looks like:

    <system.web.webPages.razor>
      <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
          <add namespace="System.Collections.Generic" />
        </namespaces>
      </pages>
    </system.web.webPages.razor>
    

    You can also use a namespace in a single file using the syntax:

    using System.Collections.Generic