MVC5视图中@model不存在于当前上下文问题求助
Hey there, let's work through this frustrating error you're hitting when trying to access your model in MVC5 views. I’ve reviewed the partial Views/Web.config content you shared, and here are targeted solutions to resolve this:
1. Complete & Verify Your Views/Web.config Settings
Your provided config is truncated (ends with Sy...), so first make sure the file is fully configured with matching versions and all necessary namespaces. Here’s the correct, full structure for MVC5:
<configuration> <configSections> <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> </sectionGroup> </configSections> <system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <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" /> <!-- Add YOUR project's namespace here so @model can find your models --> <add namespace="YourProjectName.Models" /> <add namespace="YourProjectName" /> </namespaces> </pages> </system.web.webPages.razor> <appSettings> <add key="webpages:Enabled" value="false" /> </appSettings> <system.webServer> <handlers> <remove name="BlockViewHandler"/> <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> </handlers> </system.webServer> </configuration>
Key checks here:
- Ensure all version numbers (
3.0.0.0for Razor,5.2.3.0for MVC) match what’s referenced in your project’s NuGet packages. - Add your project’s root namespace and Models namespace to the
<namespaces>section—this is often the missing piece that lets Razor find your custom models.
2. Double-Check Your View’s @model Declaration
Make sure you’re specifying the full, correct type in your view, e.g.:
@model YourProjectName.Models.Product
Avoid typos in the namespace or model name. If your model is in a nested folder, include that too (@model YourProjectName.Models.Products.Product).
3. Validate the Root Web.config Settings
Head to your project’s root Web.config and confirm these sections are present and correct:
<system.web> <compilation debug="true" targetFramework="4.7.2"> <!-- Match your target framework --> <assemblies> <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </assemblies> </compilation> <pages> <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>
This ensures the MVC assembly is properly registered across the entire project.
4. Clean & Rebuild Your Project
Sometimes cached files cause this error. Do the following:
- Right-click your solution → Clean Solution
- Delete the
binandobjfolders from your project directory - Right-click your solution → Rebuild Solution
- Restart Visual Studio to clear any leftover Razor cache.
5. Check for Invalid View Inheritance
Ensure your view isn’t manually overriding the base type incorrectly. The default should be System.Web.Mvc.WebViewPage (which matches the pageBaseType in Views/Web.config). If you have something like @inherits System.Web.WebPages.WebPage, change it back to the MVC-specific base type.
内容的提问来源于stack exchange,提问作者Manisha Bhatia




