为什么我不能在 ASP.NET MVC 2 中将 Html 帮助器方法与强类型视图一起使用?
我创建了一个带有模型的 ASP.NET MVC 2 项目。然后,我创建了一个视图,并选择它为所创建的模型强类型。我也用局部视图做了同样的事情。无论哪种情况,由于某种原因,我都会收到错误:“每当我尝试使用 ASP.NET MVC 2 提供的用于为模型创建表单元素的 Html 帮助器方法时,都无法解析符号 'Html'。我有 Visual Studio 2010 和 ASP安装了 .NET MVC 2。如果是,是否有解决方案可以解决此问题?
我的
public namespace MyNamespace { public class MyModel { public string MyProperty { get; set; } } }
是:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MyNamespace.MyModel>" %>
常规视图的第一行 我的部分观点是:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.MyModel>" %>
I created an ASP.NET MVC 2 project with a model. I then created a view and selected for it to be strongly-typed for the model that was created. I also did the same thing with a partial view. In either case for some reason I am getting the error: "Cannot resolve symbol 'Html' whenever I try to use the Html helper methods that ASP.NET MVC 2 provides for creating form elements for the model. I have Visual Studio 2010 and ASP.NET MVC 2 installed. Is this something that has been seen before? If so, is there a solution that will resolve this?
My model looks like:
public namespace MyNamespace { public class MyModel { public string MyProperty { get; set; } } }
The first line of my regular view is:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MyNamespace.MyModel>" %>
The first line of my partial view is:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.MyModel>" %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想我找到了解决您问题的方法(虽然我不确定这是否是暂时的)
当您创建一个新的 asp mvc 项目时,它包含 2 个 web.config 文件。一份位于项目根目录,一份位于 Views 目录。
我认为视图上的那个不是必需的,所以我排除了它。第二天,resharper 停止识别 Html 辅助方法。我再次添加它并重新加载解决方案,所有错误都消失了。
希望这有助于
编辑
此链接 可能会帮助解决此问题
I think I found a fix for your problem (I'm not sure if it's temporary though)
When you create a new asp mvc project it contains 2 web.config files. One on the project root and one on the Views directory.
I thought that the one on the Views wasn't required so I excluded it. The next day resharper stop recognizing the Html helper methods. I added it again and reload the solution and all errors were gone.
Hope this helps
EDIT
this link would probably help with this problem
我总是收到这个错误,我不太清楚是什么原因造成的。它一定与 Visual Studio 未能正确解析
Page
/UserControl
指令以及随后未加载继承属性中定义的正确类型有关。除了丢失智能感知之外,它不会真正影响应用程序,并且当您打开 aspx 视图文件并构建解决方案时,您还会在 Visual Studio 输出中收到很多错误(您的解决方案仍然应该构建,无需担心)。
通过修改
Page
/UserControl
指令,我可以时不时地让错误消失,这会迫使 Visual Studio 重新解析它。例如,在inherits
属性内的任意位置放置一个随机空格,然后将其删除。HTH,
查尔斯·
诗篇。我也在使用 resharper,但我不确定这是否是罪魁祸首。
I get this error all the time, I don't quite know what causes it. It must have something to do with Visual Studio failing to parse the
Page
/UserControl
directive properly and subsequently not loading the correct type defined in the inherits attribute.It doesn't really affect the application other than losing intellisense and you also get a lot of errors in the visual studio output when you have the aspx view file open and build your solution (your solution should still build no worries).
I can get the error to go away every now and again by messing with the
Page
/UserControl
directive, which forces Visual Studio to re-parse it. E.g. put a random space anywhere inside theinherits
attribute and then delete it.HTHs,
Charles
Ps. I too am using resharper, but I'm not sure if it's to blame.
是的,这种情况在我身上也发生过几次。如果应用程序正确加载并且视图加载对象,我怀疑它可能是您的 ReSharper 版本。尝试更新您的 Resharper 版本,看看它是否可以识别您的模型。
Yea, this has happened a couple times to me as well. If the application loads correctly and the view loads the objects I suspect it might be your ReSharper Build. Try updating your version of Resharper and see if it recognizes your models.
我也遇到了同样的问题,还因为一时的无知而删除了 web.config...
将下面的 web.config 片段合并到 Web 应用程序根目录中的 web.config 也可以解决问题。它再次正常工作,但您仍然只有一个 web.config 文件。
I've had the same issue, also removed the web.config in a moment of ignorance...
Merging the below piece of the web.config to your web.config in the root of the web application will do the trick also. It's works again like it should and still you have only one web.config file.
这个错误发生在我身上,因为我错误地移动了“Views”文件夹内的 web.config。
该文件包含 System.Web.Mvc 的包含内容。希望这有帮助。
问候
This error happened to me because I moved by mistake the web.config that was inside the "Views" folder.
This file contains the include of System.Web.Mvc. Hope this helps.
Greetings