I'm working on .Net project and I'm using _ViewImports.cshtml file to import namespaces for many ViewModels I have. Similar to what is described at official documentation.
Is there a way to import just parent namespace e.g. @using WebApplication1.ViewModels and have access to it's view models in razor view:
@model Home.HomepageViewModel or@model About.AboutViewModelSo far I did not manage to do it and I have a really long list of namespaces in my _ViewImports.cshtml file (don't mind much, but would be nice if possible to do just import of e.g. WebApplication1.ViewModels and have access to it's view models).
Tnx!
Razor is not that smart to understand that kind of "partial" namespaces for models.
If you define @using App.ViewModels in ViewImports, and your actual view model is in App.ViewModels.Home.HomeVM, then you can not use @model Home.HomeVM (omit App.ViewModels), Razor can't figure out it is part of App.ViewModels namespace (prefix model namespace).
I'm guessing that's because Razor can't know what prefix to use, you can have 50 @usings in ViewImports and Razor would have to go through each of them, and then if you have same class name if different namespaces, can't know which one to use ...
You have to have full paths in _ViewImports file, or full path in the view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With