0

I'm creating my first MVC application using MVC 6. While for example in Register.cshtml (which is created by default) the model is added like:

@model RegisterViewModel

But when I want to add Models that I created I have to do it like:

@model ProjectName.Models.HomeModels.MyModel

Note: I know how to import namespaces, what I want to know is that in the default views, there is no namespace import and adding models anyway, but for the models I created myself, they don't.

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • Possible duplicate of [How do I import a namespace in Razor View Page?](http://stackoverflow.com/questions/3239006/how-do-i-import-a-namespace-in-razor-view-page) – krillgar Nov 18 '16 at 12:29
  • I voted to close as a duplicate of [this question](http://stackoverflow.com/questions/3239006/how-do-i-import-a-namespace-in-razor-view-page), however the [2nd answer](http://stackoverflow.com/a/6723046/1195056) is a better solution. – krillgar Nov 18 '16 at 12:30
  • @krillgar I know how to import namespaces. my problem is that in default cshtml pages there is no namespace import and adding models work without name spaces, but not for my models – Ashkan Mobayen Khiabani Nov 18 '16 at 12:30
  • Right, because your models are in a different namespace than those used in the default pages. Show your web.config namespaces section from within the Views folders. If you're using an Area, then that has its own web.config. – krillgar Nov 18 '16 at 12:34
  • The second answer that was added, worked for me, and was what I was looking for, why did you deleted it? (the one that suggested importing namespaces in _ViewImports.cshtml file) – Ashkan Mobayen Khiabani Nov 18 '16 at 12:40
  • and please note that this is not the duplicate of [http://stackoverflow.com/questions/3239006/how-do-i-import-a-namespace-in-razor-view-page](http://stackoverflow.com/questions/3239006/how-do-i-import-a-namespace-in-razor-view-page) – Ashkan Mobayen Khiabani Nov 18 '16 at 12:43
  • I deleted nothing. You may not recognize it, but the problem set that you're experiencing is the same as the question. The fact that one of the answers there solved your problem is another case that it is a duplicate. – krillgar Nov 18 '16 at 12:44
  • @krillgar I know you didn't, the comment was for the answerer (which I didn't get what the his/her username was) – Ashkan Mobayen Khiabani Nov 18 '16 at 12:45
  • none of those answers, answered my question, there was actually an answer to this question in here, that solved my problem and when I wanted to upvoted it, I got the message that the answer is deleted – Ashkan Mobayen Khiabani Nov 18 '16 at 12:47

2 Answers2

1

I think you're asking why you have to add usings for your classes, but other Microsoft classes and such are available in the view automatically without needing usings? There's a bit of black magic you're not seeing. The good news is that you can do the same thing. If you expand the Views directory in your project, you should see a Web.config file in there. This is different from the main Web.config used for your project; this one applies just to things in the Views directory. If you open this file, you'll see a section inside like:

<system.web.webPages.razor>
  <host factoryType="..." />
  <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.Optimization"/>
      <add namespace="System.Web.Routing" />
      ...
    </namespaces>
  </pages>
</system.web.webPages.razor>

Behold: the missing usings. In case you haven't guessed, you can add your own namespaces here. Anything you add here will have the effect of automatically adding a using statement to your view for that namespace.

UPDATE

Whoops. Just noticed the core tag. I don't think this works anymore with core, but I'll leave my answer just in case someone needs the MVC method. In ASP.NET Core, you'll create a _ViewImports.cshtml file in your Views directory, instead, and add your using statements there:

_ViewImports.cshtml

@using MyProject.Models
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
0

You need to use @using command to specify the namespace. When you did this that means that you are using this namespace on your view and you can access all the method from your view but be be careful about the duplicated names in other namespaces. Now you can add the model to your view. But the @using command only can be used for namespaces.

Kewin Rather
  • 135
  • 1
  • 12