views:

38

answers:

1

Hey SO,

I'm working on an editor page for a project in ASP.NET MVC. I would like to use a control for both the create page and the edit page so I don't have to duplicate code. I've set up an EditorTemplates folder inside /Views/Shared to hold my templates. And I've placed a .ascx file in there called ArticlePresentation.ascx.

ArticlePresentation.ascx looks like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<genesis.Core.Presentation.ArticlePresentation>" %>

Testing the control

My Edit.aspx view looks like this:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/genesis.Master" Inherits="System.Web.Mvc.ViewPage<genesis.Core.Presentation.ArticlePresentation>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Edit
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Edit</h2>
    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>
        <%: Html.EditorFor(ArticlePresentation => ArticlePresentation)%>
    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

</asp:Content>

I can build the site without getting any errors, but, when I run the site and browse to the editing page I get this error:

System.Web.HttpParseException was unhandled by user code

 Message=The directive 'control' is unknown.

 Source=System.Web

 ErrorCode=-2147467259

 WebEventCode=0

 FileName=C:<path to folder>asp.net mvc\genesis\genesis\Views\Shared\EditorTemplates\ArticlePresentation.aspx

 Line=2

 VirtualPath=/Views/Shared/EditorTemplates/ArticlePresentation.aspx

 StackTrace:

      at System.Web.UI.TemplateParser.ParseString(String text, VirtualPath virtualPath, Encoding fileEncoding)

      at System.Web.UI.TemplateParser.ParseFile(String physicalPath, VirtualPath virtualPath)

      at System.Web.UI.TemplateParser.ParseInternal()

      at System.Web.UI.TemplateParser.Parse()

      at System.Web.Compilation.BaseTemplateBuildProvider.get_CodeCompilerType()

      at System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider)

      at System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders()

      at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()

etc... etc...etc...

I've searched google looking for solutions, but everything I've found so far relates to having the control in an aspx page rather than a ascx page.

Does anyone know what could be causing this error?

+1  A: 

The reason you are getting this error is because your file has an .aspx extension and not .ascx. Read the error message carefully:

ErrorCode=-2147467259

WebEventCode=0

FileName=C:\Documents and Settings\bquakkelaar\Desktop\dropstuff\asp.net mvc\genesis\genesis\Views\Shared\EditorTemplates\ArticlePresentation.aspx

Now rename the file to ArticlePresentation.ascx and everything will work as expected.

Also you could replace this line:

<%: Html.EditorFor(ArticlePresentation => ArticlePresentation)%>

with this:

<%: Html.EditorForModel() %>
Darin Dimitrov
Hey Darin - thanks a lot for the help and tips. I've noticed you've helped me out before with some great advice. I am very confused with this one becaue when I use the solution explorer to browse to the \Views\Shared\EditorTemplates\, the only file in there is called ArticlePresentation.ascx. I don't understand why the error says says that it's an aspx
quakkels
Is there a way to jolt VWD 2010 Express into re-assesing the files in that folder?
quakkels
Thanks for that attention to detail Darin. Apparently, I had 2 files in there (an aspx and an ascx) but the solution explorer was only displaying the ascx. I deleted the aspx using windows explorer and built the project again. viola! it werkd. Thanks
quakkels