Ben Finkel
Ben Finkel

Reputation: 4803

MVC Project errors after adding .ascx control

I've tried to add a .ascx user control to my EditorTemplates but doing so causes a hundred erros to pop up in my MVC 3 project. Every reference to "System." errors as "is not defined" and the compiler wants me to update them to "Global.System."

Can anyone tell me why adding the user control does this?

Thank you

UPDATE:

If i take out the code-behind files that are automatically created (and I don't think I need) then the problems seem to resolve. Very interesting...

Upvotes: 0

Views: 1130

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

User controls (stuff with runat="server") is not something that should be used in ASP.NET MVC application. They usually rely on Postbacks and ViewState which are notions that no longer exist in ASP.NET MVC. You could use an ascx partial/editor template and invoke it from a Razor view. So for example let's suppose that you have the following partial:

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

<%= Html.LabelFor(x => x.Foo) %>
<%= Html.TextBoxFor(x => x.Foo) %>

You could include it from a Razor view like this:

@Html.Partial("NameOfThePartial")

Upvotes: 1

Related Questions