1

Sorry for my english, it is not the native language for me.

I'm writing a web access to a large medical system.

Using ASP.NET MVC 4. It so happened that the models and controllers are in the other dll, what loaded when web-application started. Controllers are registered in the Unity container, and everything work fine.

But in the View does not work use the model classes directly. Razor view engine can not find their namespace.

How to fix it? How can you make use ViewEngine namespace library that is loaded into memory, but on which there is no reference of mvc-project?

Thank you in advance for your advice.

Bridge
  • 29,818
  • 9
  • 60
  • 82
Alexey Nikiforov
  • 357
  • 1
  • 4
  • 14
  • 1
    See http://stackoverflow.com/questions/4953330/razor-based-view-doesnt-see-referenced-assemblies You can configure the namespaces of the View in a new config section. Also http://forums.asp.net/t/1779588.aspx/1 might help. – Gabe Thorns Oct 29 '12 at 15:44

1 Answers1

2

There are two methods in which you can add a namespace to a Razor view in ASP.NET MVC.

Method 1

The first way is to use the @using statement in .cshtml files, which imports a namespace to the current file only.

Example :-

@using Namespace1
@using Namespace2.SomeClass

Method 2

and the second way is to define these namespaces in the web.config of the View directory of your project.

enter image description here

Example:-

<system.web.webPages.razor>
<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.Routing" />
    .
    <!-- can more more here... -->
  </namespaces>
</pages>

or you can add your custom namespace like this too :

<add namespace="Custom.Yasser" />
<add namespace="Custom.Mohsin" />

Source

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
  • Thanks for answer, but this will not work, because my namespace in library, that not referenced in mvc-project. In moment view interpretation show error. – Alexey Nikiforov Oct 30 '12 at 11:42