Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9251405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:41:25+00:00 2026-06-18T10:41:25+00:00

I’m trying to use a model from a custom namespace in my .cshtml file.

  • 0

I’m trying to use a model from a custom namespace in my .cshtml file. However, whenever I try to set the model, Razor throws a The type or namespace name could not be found error.

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

Here’s what the code in the MyViewModel.cs file looks like:

namespace MyProject.ViewModels
{
    public class MyViewModel
    {
    }
}

Alright, so that is the basic layout. The problem is that when I try to use the MyViewModel object in my Razor view, Razor says that it can’t find the namespace. As shown here:
Razor cannot find the namespace

I believe I’m doing everything the way I should. It just seems like Razor is failing to look in the solution folders. How can I get the MyViewModel object to be recognized by Razor? Is there something I am missing?

Please note: I do realize that I could simply change my @using statement to reference the entire path. However, in my project I have many solution folders inside the ViewModels folder and I do not wish to go through and specify each one.

Update

I’ve accepted Darin’s answer because it successfully answers the question. I’ve also provided some more information below about my specific situation and why this question came up.

I looked into the issue a bit more and figured out what was going on. I normally use the SparkViewEngine which allows for referencing my view models like this: <viewdata model="ViewModels.MyViewModel"/> When Razor did not allow me to reference the view models in the same manner I thought there was something wrong with Razor.

Each ViewEngine creates temporary CS files which are used to help generate the view. The temporary files created by Spark use the “controllers” namespace. In this case Spark would have used the MyProject.Controllers namespace in it’s temporary CS file. This means that I can reference the ViewModels.MyViewModel object without specifying the full namespace because it is in the same namespace as the generated CS file.

The temporary files that are generated by Razor use the ASP namespace. This means that I cannot reference the ViewModels.MyViewModel object without specifying the full namespace because it is not in the same namespace as the generated CS file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:41:26+00:00Added an answer on June 18, 2026 at 10:41 am

    Add the namespace in which the your view model is declared to the top of your razor view using the @using directive:

    @using MvcApplication1.ViewModels
    @model MyViewModel
    

    Also to avoid adding this in every single razor view you would like to use it, you could add it to the <namespaces> section of your ~/Views/web.config file (do not 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 have specified the full namespace. In the screenshot you have shown you seem to only be using ViewModels.MyViewModel but are you sure that this is the correct namespace? Usually when you add a class file in Visual Studio it will use the application name + folder to prefix the namespace like MvcApplication1.ViewModels.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.