Asp.Net Mvc: Cannot access my models namespace from my view
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".
Related videos on Youtube
Comments
-
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 over 13 yearsBeen compiled several time and is public.
-
Justin Soliz over 13 yearsThats what I was referring to when I created a "Global" namespace, but it is added in my namespaces of my Web.confing
-
Giovanny over 13 yearsIt 'should' work. Try adding an explicit include statement in the view itself: <%@ Import Namespace="Web.Models" %>
-
Justin Soliz over 13 yearsI've tried that as well. I have 4 projects in the solution, I'm wondering if I'm missing something in that regard.
-
Giovanny over 13 yearsIs the view in a different project to the models? Have you added the project reference between projects? (clutching at straws now!)
-
Justin Soliz over 13 yearsI 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 almost 9 yearsI 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 over 8 yearsThe 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 over 5 yearsSaved my life! Many Thanks!