简体   繁体   中英

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

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?

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:

<%@ 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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