简体   繁体   English

为什么我的ascx文件不能在属性后面提取我的代码?

[英]Why won't my ascx file pick up my code behind properties?

My web app is a solution built on top of another solution. 我的Web应用程序是在另一个解决方案之上构建的解决方案。 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. 我在类后面的代码中使用ViewUserControl<SomeViewModel> In my code behind, I have access to this.Model and all the other goodies I'd expect from this class. 在后面的代码中,我可以访问this.Model和我希望从此类中获得的所有其他好处。 But in my ascx file, I get red squiggley lines when I try to access this.Model or other properties defined in ViewUserControl . 但是在我的ascx文件中,当我尝试访问this.ModelViewUserControl定义的其他属性时,会出现红色的波浪线。 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. 但是我的ascx文件认为会有错误。 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. 无论如何,它都不是表演的制胜法宝,但是如果我要使用具有智能感知和所有爵士乐的IDE,我肯定希望它能正常工作,并选择应该存在的属性。

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: 这是我的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. 对于那些告诉我ASP.NET MVC无法或不使用背后代码的人,很抱歉,但这是曲棍球。 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. 实际上,它在我的产品解决方案中也可以在运行时很好地工作,只是我的问题是我的ascx文件似乎不了解背后的代码。 Furthermore, ViewUserControl is a System.Web.MVC class. 此外, ViewUserControlSystem.Web.MVC类。 Are you still going to tell me that code behind isn't used in ASP.NET MVC? 您是否仍要告诉我,ASP.NET MVC中没有使用背后的代码?

Since you are developing MVC you cannot use code behind. 由于您正在开发MVC,因此不能在后面使用代码。 Instead, what do you think about adding this at the top of your .acsx file: 相反,您如何考虑将其添加到.acsx文件的顶部:

<%@ Import Namespace="Namespace.Model" %>

Then you can access everything you have in there without so much complication. 然后,您可以轻松地访问其中的所有内容。

ASP.NET MVC does not use code behind. ASP.NET MVC不使用背后的代码。 It sounds like you are mixing ASP.NET MVC and ASP.NET WebForm pages. 听起来您好像混用了ASP.NET MVC和ASP.NET WebForm页面。

I suggest you take a look at http://www.asp.net/mvc . 我建议您看看http://www.asp.net/mvc It has some great tutorials on getting started with MVC and how it works. 它有一些很棒的教程,介绍了MVC入门及其工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM