Joe
Joe

Reputation: 834

Resolve Error in MVC 4 Razor

in .cs files when you encounter errors like "The type or namespace name 'Products' could not be found (are you missing a using directive or an assembly reference?)"

you right click it and select resolve to automatically add missing assembly references. in razor .cshtml, how do you do this?

Upvotes: 0

Views: 213

Answers (1)

Shyju
Shyju

Reputation: 218732

You add a using statement to your razor view.

@using YourNameSpaceIfExists.Products

If you do not wish to type the full path(NamespaceName.className) in each view you want to use this class, you can globally add those namespace to the web.config available in the Views folder. You will see a namespaces secrion under pages section. Add your namespace to the l

<system.web.webPages.razor>
  <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <add namespace="System.Web.Mvc" />
      <add namespace="System.Web.Mvc.Ajax" />
      <add namespace="System.Web.Mvc.Html" />
      <add namespace="System.Web.Optimization"/>
      <add namespace="System.Web.Routing" />
      <add namespace="YourNameSpaceNameHere"/>
    </namespaces>
  </pages>
</system.web.webPages.razor>

Now in your views, you can simply use the class name

@model Product

Upvotes: 1

Related Questions