MVC Bug: Broken Codebehind

December 11, 2007 1:56 PM

Update (Jan 2): I have posted a permanent fix in the form of modified Visual Studio templates in another blog post. 

Update (Dec 11): ScottGu was kind enough to post a fix for this particular problem. It is unfortunate that you have to repeat the fix for every view you add to your project, but I guess that's why it is called a CTP! ScottGu said...

There is a bug in the file template when you create a new page - and the .designer.cs file isn't generated.
To fix this, right click on the file and choose the "Convert to Web Application" menu item. This will generate the .designer.cs file that contains your control declarations. From that point on the code-behind will be kept up to date as you make changes.


Hope this helps,


Scott



Original Post: 

It may not be a popular choice, but I'm perfectly okay with in-line code in my views as long as it doesn't contain business logic. One of the developers on my project prefers tag-based views (<asp:Blah runat="server" />) and came to me yesterday with a curious issue. I've spent some of the morning investigating the issue and it does appear that there is a bug in the newly released ASP.Net MVC Framework.

The issue? Controls declared in an ASPX are not visible to that page's codebehind.

To test this hypothesis (and make sure we hadn't somehow broken our project) I started a new "ASP.Net MVC Web Application" project (note that the bug also exists for the "ASP.Net MVC Web Application and Test" project). I then opened the Views/Home/About.aspx file and added the following line:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="About.aspx.cs" Inherits="MVCBug.Views.Home.About" %>

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

    <h2>Todo: Company information</h2>

    <asp:Literal ID="myLiteral" runat="server" Text="Hello World" />

</asp:Content>

 

Then I opened up the page's codebehind (Views/Home/About.aspx.cs) and added the following:

using System;

using System.Web;

using System.Web.Mvc;

namespace MVCBug.Views.Home

{

    public partial class About : ViewPage

    {

        public void Page_Load()

        {

            myLiteral.Text = "Goodbye World";

        }

    }

}

 

Ctrl+Shift+B to compile and bam, a build error: "The name 'myLiteral' does not exist in the current context."

I began to wonder if this was not supported by the MVC framework, but I took a look back at one of ScottGu's most recent articles, ASP.Net MVC Framework (Part 3): Passing ViewData from Controllers to Views, and saw that he references controls from the page's codebehind several times. I also tried using several controls besides the Literal to no avail.

To test what was going on, I commented out the reference in the Page_Load method, added a string declaration and put a breakpoint on it. When I ran the MVC application in debug mode and loaded the page I was able to see the "myLiteral" control reference. It appears that the reference is available at runtime but Visual Studio just is not able to see it at compile time. Odd.

DebugMyLiteral

For now I've told the developer to just use in-line code, but I'm well aware that many developers are loathe to do so. Thoughts? Suggestions?

Tags: , ,
Categories: MVC
Actions: E-mail | Permalink | Comments (6) RSS Feed for this post's comments.

Comments

Comments are closed

Troy Goode

Troy Goode
Microsoft Certified Professional Developer
AddThis Feed Button

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008

Colophon

Powered by:
BlogEngine.NET 1.4.5
Template:
Designs by Darren
Header Font:
Stamper
Syntax Highlighting:
WLW Code Snippet Plugin
Filter by APML