Asp.Net Mvc: Cannot access my models namespace from my view

12,319

Solution 1

You can add it in your web.config under system.web/pages/namespaces. E.g.,

...
<namespaces>
    ...
    <add namespace="Web.Models"/>
</namespaces>
...

Solution 2

First, compile your app, then make sure that MyViewModel is public.

Solution 3

Found out it had to do with my build of ReSharper, I updated to 5.1 and it took care of what I guess was some sort of a "cache bug".

Share:
12,319

Related videos on Youtube

Justin Soliz
Author by

Justin Soliz

I'm a software developer at Avalara.

Updated on May 03, 2022

Comments

  • Justin Soliz
    Justin Soliz almost 2 years

    So I have a ViewModel in the 'models' folder of my Mvc project with a namespace of 'Web.Models' (My Mvc project is called 'Web') I think its worth mentioning I have 3 other projects in my solution: Domain, Test, and Tasks. The view model is assigned properties from classes in my Domain.Entities folder. I can create a new instance of my viewmodel in my controller when I add the namespace in my contoller.

    using Web.Models;
    

    When I create the view however, it cant seem to import the namepace. It actually prompts me to add the namespace via 'alt+ enter' or 'ctrl + dot' and it still says that it cant resolve the object.

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Web.Models.MyViewModel>" %>
    

    I've also tried adding a global reference to the namespace in my Web.config, but no luck. Any suggestions?

  • Justin Soliz
    Justin Soliz over 13 years
    Been compiled several time and is public.
  • Justin Soliz
    Justin Soliz over 13 years
    Thats what I was referring to when I created a "Global" namespace, but it is added in my namespaces of my Web.confing
  • Giovanny
    Giovanny over 13 years
    It 'should' work. Try adding an explicit include statement in the view itself: <%@ Import Namespace="Web.Models" %>
  • Justin Soliz
    Justin Soliz over 13 years
    I've tried that as well. I have 4 projects in the solution, I'm wondering if I'm missing something in that regard.
  • Giovanny
    Giovanny over 13 years
    Is the view in a different project to the models? Have you added the project reference between projects? (clutching at straws now!)
  • Justin Soliz
    Justin Soliz over 13 years
    I know its strange.... the really weird thing is the class is available in the controller but not in the view. I can get it to work, I've changed the namespace to system.web.mvc as well as moving it over to my domain project and changing the namespace to domain.models. I guess its just more of an annoyance at this point (and really strange, never happened to me before).
  • Cody
    Cody almost 9 years
    I cleared the cache for Resharper, so this was my issue too. I had recently moved the view model namespaces to the web config, and it didn't like it.
  • Catto
    Catto over 8 years
    The Model needs to be a public class! So simple & makes sense now you pointed that out. It is one cause of the error. Thank you very much for the answer.
  • SamuraiJack
    SamuraiJack over 5 years
    Saved my life! Many Thanks!