为什么我无法在用户控件中访问我的视图模型?

发布于 2024-10-19 16:39:10 字数 797 浏览 0 评论 0原文

我以几种不同的方式尝试过:

1:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyNameSpace.MyModel>" %>

2:

<%@ Import Namespace="MyNameSpace.MyModel" %>

但无论我做什么,我都无法访问 this.Model 或我希望看到的任何其他内容。项目中的其他控件和页面能够访问这些控件和页面,但没有一个开发人员能够告诉我为什么他们可以而我不能。有什么想法吗?

编辑

从那时起我创建了一些控件和页面。有时,智能感知会毫无问题地找到 this.Model。它还可以找到诸如 Html.RenderPartial() 之类的东西以及它应该找到的各种其他好东西。因此,对于那些告诉我智能感知在视图中不起作用的人来说,你错了。确实如此。但并非所有观点都一致。据我和其他开发人员所知,我没有做任何不同的事情。我尝试过的事情:

  • 关闭解决方案
  • 删除构建后文件夹
  • 重新打开解决方案
  • 重新构建,无论是常规构建还是分析构建

有时这些步骤可以解决问题,但对于某些控件,我就是无法使红色波浪线消失!这不是世界末日,因为它不会产生运行时错误,但我想了解这里发生了什么。肯定有人经历过这种情况并了解问题的根源。

I've tried this in a couple different ways:

1:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyNameSpace.MyModel>" %>

2:

<%@ Import Namespace="MyNameSpace.MyModel" %>

But no matter what I do, I don't have access to this.Model or anything else I'd expect to see. Other controls and pages in the project are able to access these, and none of the developers are able to tell me why theirs can and my can't. Any ideas?

EDIT

I've created a few controls and pages since then. Sometimes, intellisense finds this.Model with no problem. It also finds things like Html.RenderPartial() and various other goodies that it's supposed to find. So, for those of you who would tell me that intellisense doesn't work in views, you're wrong. IT DOES. But it's not consistent amongst all views. As far as I and any other developers can tell, I'm not doing anything different. Things I've tried:

  • Close the solution
  • Delete post build folders
  • Reopen solution
  • Rebuild, either regular or analysis build

Sometimes those steps fix the problem, but for some controls, I just can't make the red squiggley lines go away! It's not the end of the world, since it doesn't produce runtime errors, but I want to understand what's going on here. Surely somebody has experienced this and has learned the source of the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹苦笑 2024-10-26 16:39:10

您还可以尝试将其添加到 web.config :

  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="MyHtmlHelperLibrary.HtmlExtensions"/>
    <add namespace="MyOtherNamespace"/>
  </namespaces>

You can also try adding it to the web.config :

  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="MyHtmlHelperLibrary.HtmlExtensions"/>
    <add namespace="MyOtherNamespace"/>
  </namespaces>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文