Does Razor look like in solution folders?

I am trying to use a model from a custom namespace in my .cshtml file. However, when I try to install the model, Razor throws an error The type or namespace name could not be found

.

I have a project setup that uses solution folders to help with organization as shown below: Shows the FolderStructure of MyProject

This is what the code looks like in the MyViewModel.cs file:

namespace MyProject.ViewModels
{
    public class MyViewModel
    {
    }
}

      

Ok, so this is the main layout. The problem is that when I try to use an object MyViewModel

in my Razor view, Razor says it cannot find the namespace. As shown here: Razor cannot find the namespace

I believe that I am doing everything the way I should. Razor doesn't seem to look in solution folders. How can I get the object MyViewModel

to be recognized by Razor? Is there something I am missing?

Please note, I understand that I can just change the instruction @using

to reference the whole path. However, in my project, I have many solution folders in the ViewModels folder, and I don't want to go through and list each one.

Update

I accepted Darin's answer because he successfully answers the question. I have also provided more details on my specific situation and why this question came up.

I did some research on this question and figured out what was going on. I usually use SparkViewEngine, which allows me to reference my view models like this: <viewdata model="ViewModels.MyViewModel"/>

When Razor wouldn't let me reference view models in the same way, I thought there was something wrong with Razor.

Each ViewEngine creates temporary CS files that are used to create the view. Temporary files created by Spark use the controllers namespace. In this case, Spark would use the namespace MyProject.Controllers

in its temporary CS file. This means that I can refer to the object ViewModels.MyViewModel

without specifying the full namespace, since it is in the same namespace as the generated CS file.

Temporary files created by Razor use the namespace ASP

. This means that I cannot refer to the object ViewModels.MyViewModel

without specifying the full namespace, because it is not in the same namespace as the generated CS file.

+3


source to share


2 answers


Add a namespace where your view model is declared at the top of your razor with the directive @using

:

@using MvcApplication1.ViewModels
@model MyViewModel

      

Also, not to add this to every kind of razor you would like to use it, you can add it to <namespaces>

your file section ~/Views/web.config

(don't confuse with ~/web.config

):



<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.Routing" />

        <add namespace="MvcApplication1.ViewModels" />
      </namespaces>
    </pages>
</system.web.webPages.razor>

      

Also notice how I specified the full namespace. In the screenshot you showed, you are only using ViewModels.MyViewModel

, but are you sure this is the correct namespace? Usually when you add a class file in Visual Studio, it will use the application name folder + for the namespace prefix, eg MvcApplication1.ViewModels

.

+4


source


The directive @using

must point to your namespace:

@using MyProject.ViewModels

      

And then declare your model as a class:



@model MyViewModel

      

Or, use the fully qualified name directly:

@model MyProject.ViewModels.MyViewModel

      

+1


source







All Articles