当前上下文中不存在名称“ViewBag" - Visual Studio 2015 [英] The name 'ViewBag' does not exist in the current context - Visual Studio 2015

查看:43
本文介绍了当前上下文中不存在名称“ViewBag" - Visual Studio 2015的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再次开始在 ASP.NET 中进行开发,但在 Visual Studio 中遇到了一个小错误.我的 .cshtml 文件在使用一些剃刀功能时显示错误.例如当前上下文中不存在名称‘ViewBag’".这是一张图片:

I'm starting to develop in ASP.NET again and I ran into a small error within Visual Studio. My .cshtml files show errors when using a few razor functions. For example "The name 'ViewBag' does not exist in the current context". Here is a picture:

我正在使用一个演示项目.您可以在此处找到该项目:https://github.com/Wintellect/Angular-MVC-Cookbook/tree/master/BasicProject

I am using a demo project. You can find the project here: https://github.com/Wintellect/Angular-MVC-Cookbook/tree/master/BasicProject

我查看了其他几个线程,大多数建议更新 web.config 文件.这 2 个配置文件已经存在,因为它是一个非常流行的演示,我假设它包含所有必需的配置.我当然已经查看了这些配置文件,它们确实包含了建议的解决方案.

I have looked through several other threads and most suggest to update the web.config file(s). These 2 config files are already present and since it's a pretty popular demo I assume it has all the required configuration in it. I have of course looked through these config files and they do indeed include the suggested solutions.

其他细节:

  • 我已经使用过 clean &在解决方案上重建,但这没有任何改变.
  • 当我创建一个全新的 MVC 项目时它确实有效
  • 我的朋友遇到了同样的问题,我们都使用 VS 2015 和 Windows 10
  • 我仍然可以运行该应用程序并且它确实可以工作.

提前致谢.

推荐答案

尽管配置正确,但我还是遇到了这个问题.

I had this issue despite having all the correct configuration.

结果是一些组件缓存中的坏文件,阻止 Razor 视图识别 ViewBag、Model 和 HtmlHelpers.删除这些文件解决了问题(下次打开 Visual Studio 时会创建这些文件的良好版本).

It turned out to be some bad files in the Component Cache, preventing the Razor views from recognising ViewBag, Model, and HtmlHelpers. Deleting these files solved the problem (good versions of these files were created next time I opened Visual Studio).

文件位于此处:

%LOCALAPPDATA%MicrosoftVisualStudio14.0ComponentModelCache

删除所有四个文件:

  • Microsoft.VisualStudio.Default.cache
  • Microsoft.VisualStudio.Default.catalogs
  • Microsoft.VisualStudio.Default.err
  • Microsoft.VisualStudio.Default.external

我随后在其他几台开发人员机器上看到了同样的问题,此修复程序很快就解决了.

I have subsequently seen the same issue on several other developer machines and this fix quickly solves it.

这篇关于当前上下文中不存在名称“ViewBag" - Visual Studio 2015的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆