简体   繁体   English

剃刀视图查找没有完整路径的ViewModel

[英]Razor view finding ViewModel without full path

Still new to ASP.NET, so forgive me for the simplicity of my question. 对于ASP.NET还是一个新手,所以请原谅我的问题的简单性。

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: 我正在用ASP.NET MVC Core 2编写演示应用程序。对于我的视图,我引用了ViewModel的完整路径,例如:

@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: 我要遵循的一个解决方案是使用一些voodoo,它引用的ViewModels没有完整的路径:

@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. 我没有看到任何明显的线索。

This likely is because the demo project will have added the namespace. 这可能是因为演示项目将添加名称空间。 It's done in the web.config, usually inside the Views folder. 通常在Views文件夹中的web.config中完成此操作。 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. 对于ASP.Net Core,导入是在Views文件夹中的_ViewImports.cshtml文件中完成的。 Add a line like this: 添加这样的一行:

@using MyApp.ViewModels

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM