为什么我的 ascx 文件无法获取属性背后的代码?
我的网络应用程序是一个构建在另一个解决方案之上的解决方案。基本解决方案旨在成为多种产品的平台。在大多数情况下,这工作得很好,但当我创建特定于产品的视图时,会发生奇怪的事情。
我正在使用 ViewUserControl
作为类背后的代码。在我的代码后面,我可以访问 this.Model 以及我期望从此类中获得的所有其他好处。但在我的 ascx 文件中,当我尝试访问 this.Model
或 ViewUserControl
中定义的其他属性时,会出现红色波浪线。当我尝试访问直接在后面的代码中定义的属性时,我也会得到红色的波浪线。
更有趣的是,我没有从中得到任何真正的错误。该视图在运行时渲染得很好,并且不会给我任何构建错误。但我的 ascx 文件认为会有错误。如果我在平台命名空间中创建完全相同的视图,它就可以正常工作。没有红色曲线。
我觉得这真的很烦人。无论如何,它都不是一个表演障碍,但如果我要使用具有智能感知和所有这些爵士乐的 IDE,我肯定希望它能够正常工作,并获取应该存在的属性。
还有其他人遇到过这个吗?你知道有办法解决这个问题吗?
谢谢!
编辑
有人要求我发布一些代码。这是背后的代码:
namespace MyProject.MyProduct.Web.Views
{
public class EditSearch : ViewUserControl<SearchResultsViewModel>
{
public bool IsSearchTypeA()
{
...............
}
public bool IsSearchTypeB()
{
...............
}
}
}
这是我的 ascx:
<%
if (!this.IsSearchTypeB())
{
string categoryPageTitle = this.Model.SearchWidgetParameters.Search.CategoryPageTitle;
string categoryPageUrl = this.Model.SearchWidgetParameters.Search.Filters.CategoryPageUrl;
if (!string.IsNullOrEmpty(categoryPageUrl))
{
%> <div id="coid_website_backtoCategoryPageLink"> <%
string tocGuid = this.Model.SearchWidgetParameters.Search.Filters.TocGuid;
if (!string.IsNullOrEmpty(tocGuid))
{
categoryPageUrl += "?guid=" + tocGuid;
}
var backToLink = new HyperLink();
if (this.IsSearchTypeA())
{
backToLink.Text = "Edit Search";
}
else
{
backToLink.Text = "Back to " + TranslatedHtmlTextWriter.Translate(categoryPageTitle);
}
backToLink.NavigateUrl = TransactionOperations.AddContextToUrl(categoryPageUrl.StartsWith("/Browse/") ? categoryPageUrl : "/Browse/" + categoryPageUrl,
WebsiteTransitionType.Default, // Requested: CategoryPage
TransactionOperations.DefaultContextData);
backToLink.RenderControl(this.Writer);
%>
</div>
<%
}
}
%>
编辑
对于那些告诉我 ASP.NET MVC 不能或不使用背后代码的人,我很抱歉,但这只是马曲棍球。该项目已经存在多年,并且有一个广泛使用的产品在其上运行。我的产品最近才进入该平台。平台解决方案在各处使用代码隐藏,并且运行良好。事实上,它在我的产品解决方案中运行时也工作得很好,我只是有一个问题,我的 ascx 文件似乎不知道我背后的代码。此外,ViewUserControl
是一个System.Web.MVC
类。您是否仍然要告诉我 ASP.NET MVC 中不使用后台代码?
My web app is a solution built on top of another solution. The base solution is meant to be a platform for multiple products. For the most part, this works fine, but weird things happen when I create product specific views.
I am using ViewUserControl<SomeViewModel>
for my code behind class. In my code behind, I have access to this.Model
and all the other goodies I'd expect from this class. But in my ascx file, I get red squiggley lines when I try to access this.Model
or other properties defined in ViewUserControl
. I also get red squiggley lines when I try to access properties defined directly in my code behind.
What's more interesting is, I don't get any real errors from this. The view renders just fine at run time and does not give me any build errors. But my ascx file thinks there will be errors. If I create the exact same view in the platform namespaces, it works fine. No red squiggles.
I find this to be really annoying. It's not a show stopper by any means, but if I'm going to be using an IDE with intellisense and all that jazz, I'd sure like it to work properly, and pick up the properties that are supposed to be there.
Has anyone else run into this? Do you know of a way to fix this?
Thanks!
Edit
It was requested that I post some code. Here's the code behind:
namespace MyProject.MyProduct.Web.Views
{
public class EditSearch : ViewUserControl<SearchResultsViewModel>
{
public bool IsSearchTypeA()
{
...............
}
public bool IsSearchTypeB()
{
...............
}
}
}
And here's my ascx:
<%
if (!this.IsSearchTypeB())
{
string categoryPageTitle = this.Model.SearchWidgetParameters.Search.CategoryPageTitle;
string categoryPageUrl = this.Model.SearchWidgetParameters.Search.Filters.CategoryPageUrl;
if (!string.IsNullOrEmpty(categoryPageUrl))
{
%> <div id="coid_website_backtoCategoryPageLink"> <%
string tocGuid = this.Model.SearchWidgetParameters.Search.Filters.TocGuid;
if (!string.IsNullOrEmpty(tocGuid))
{
categoryPageUrl += "?guid=" + tocGuid;
}
var backToLink = new HyperLink();
if (this.IsSearchTypeA())
{
backToLink.Text = "Edit Search";
}
else
{
backToLink.Text = "Back to " + TranslatedHtmlTextWriter.Translate(categoryPageTitle);
}
backToLink.NavigateUrl = TransactionOperations.AddContextToUrl(categoryPageUrl.StartsWith("/Browse/") ? categoryPageUrl : "/Browse/" + categoryPageUrl,
WebsiteTransitionType.Default, // Requested: CategoryPage
TransactionOperations.DefaultContextData);
backToLink.RenderControl(this.Writer);
%>
</div>
<%
}
}
%>
Edit
For those of you who are telling me that ASP.NET MVC cannot or does not use code behind, I'm sorry but this is horse hockey. This project has existed for years and there is a widely used product that runs on it. My product is only just recently jumping onto the Platform. The Platform solution uses code behind all over the place, and it works fine. In fact, it works fine at runtime in my product's solution as well, I just have the problem that my ascx file doesn't seem to know about my code behind. Furthermore, ViewUserControl
is a System.Web.MVC
class. Are you still going to tell me that code behind isn't used in ASP.NET MVC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您正在开发 MVC,因此您不能使用代码隐藏。相反,您如何看待将其添加到 .acsx 文件的顶部:
然后您就可以访问其中的所有内容,而无需那么复杂。
Since you are developing MVC you cannot use code behind. Instead, what do you think about adding this at the top of your .acsx file:
Then you can access everything you have in there without so much complication.
ASP.NET MVC 不使用代码隐藏。听起来您正在混合 ASP.NET MVC 和 ASP.NET WebForm 页面。
我建议您查看 http://www.asp.net/mvc。它有一些关于 MVC 入门及其工作原理的精彩教程。
ASP.NET MVC does not use code behind. It sounds like you are mixing ASP.NET MVC and ASP.NET WebForm pages.
I suggest you take a look at http://www.asp.net/mvc. It has some great tutorials on getting started with MVC and how it works.