MVC5 login.cshtml文件ViewModels文件夹引用缺失问题咨询
Hey there! Let's walk through the most common culprits behind that missing reference error for your ViewModels folder in login.cshtml—I’ve helped tons of devs work through this exact issue:
Mismatched Namespaces
Double-check the namespace of yourLoginViewModelclass. If your class lives underYourProjectName.ViewModelsbut your view uses@model ViewModels.LoginViewModel(without the project name prefix), the compiler won’t find it. Fix this either by adding a full namespace reference at the top of your view:@using YourProjectName.ViewModelsOr by specifying the complete path in the
@modeldirective:@model YourProjectName.ViewModels.LoginViewModelViewModels Folder/Class Not Included in Project
If you manually added the ViewModels folder orLoginViewModel.csfile, Visual Studio might not have recognized it as part of the project. Right-click the ViewModels folder and select "Include in Project". Then, right-clickLoginViewModel.cs, go to Properties, and make sure the Build Action is set to "Compile" (this ensures the class gets built into your project’s assembly).Unresolved Compilation Errors Blocking References
If there are other syntax or build errors in your project (like a missing semicolon in another class), Visual Studio might fail to generate the correct assembly—this breaks reference resolution for your ViewModels first. Head to the Build panel, fix all listed errors, then rebuild your solution.Missing Namespace in Views Web.config
For global access to your ViewModels across all views, check theWeb.configfile inside your Views folder. Look for the<namespaces>section under<system.web.webPages.razor>→<pages>and add your ViewModels namespace:<add namespace="YourProjectName.ViewModels" />This lets you use
@model LoginViewModeldirectly in any view without adding@usingevery time.Visual Studio IntelliSense/Cache Glitch
Sometimes VS just gets stuck. Try these quick fixes:- Go to Build → Clean Solution
- Restart Visual Studio
- Go to Build → Rebuild Solution
If that doesn’t work, right-click your project, select "Unload Project", then right-click again and "Reload Project" to refresh the project structure.
内容的提问来源于stack exchange,提问作者Gini Gal

