forcequitIO
forcequitIO

Reputation: 572

Razor view finding ViewModel without full path

Still new to ASP.NET, so forgive me for the simplicity of my question.

I'm writing a demo app in ASP.NET MVC Core 2. For my Views, I'm referencing the full path to the ViewModel, for example:

@model MyApp.ViewModels.ProductDetailViewModel

A solution that I'm following along with is using some voodoo whereby it's referencing its ViewModels without the full path:

@model ProductDetailViewModel

I don't mind using the full path, I'm just curious as to how the demo project is doing it. I'm not seeing any obvious clues as to how it's done.

Upvotes: 2

Views: 601

Answers (1)

DavidG
DavidG

Reputation: 118977

This likely is because the demo project will have added the namespace. It's done in the web.config, usually inside the Views folder. There will be a line like this:

<system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
            <add namespace="MyApp.ViewModels" /> <!--- this line

For ASP.Net Core, the import is done in the _ViewImports.cshtml file which is in the Views folder. Add a line like this:

@using MyApp.ViewModels

Upvotes: 3

Related Questions