views:

1127

answers:

2

We have two separate front end projects for the same company which are basically the same except for all the html and css. (Different divisions within the same company) I'm trying to add a page that was built in one over to the other. (Yes, yes, I know we probably should've built a single app that display different presentations based on which division's instance was running so that we wouldn't have to maintain two separate but the same codebases, but we just can't go there with this client.)

Anyway, I copied over the controller, the model, and the aspx and ascx pages. All I needed to change was the name on the root namespace. For some reason, a particular ascx page that compiles successfully in the first project, fails in the second project.

Here's the error message:

    e:\pathtocode\Web\Views\EmailToFriend\Email.ascx(24): error CS1061: 
'System.Web.Mvc.HtmlHelper<MainWeb.Models.EmailToFriend>' does not contain a definition for
 'TextBox' and no extension method 'TextBox' accepting a first argument of type 
'System.Web.Mvc.HtmlHelper<MainWeb.Models.EmailToFriend>' 
could be found (are you missing a using directive or an assembly reference?)

Here's the code for the ascx:

 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Form.ascx.cs"
Inherits="MainWeb.Views.EmailToFriend.Form" %>

<%@ Import Namespace="System.Web.Mvc" %>

<table>
    <span class="error" style="color: red;">
        <%= Model.ErrorString %>
    </span>
    <tr>
        <td>Friend's Name: </td>
        <td><%= Html.TextBox("RecipientName", Model.RecipientName)%></td>
    </tr>
    <tr>
        <td>Friend's Email Address: </td>
        <td><%= Html.TextBox("RecipientAddress", Model.RecipientEmail)%></td>
    </tr>
    <tr>
        <td>Your Name: </td>
        <td><%= Html.TextBox("SenderName", Model.SenderName ?? UserName)%></td>
    </tr>
    <tr>
        <td>Your Email Address: </td>
        <td><%= Html.TextBox("SenderAddress", Model.SenderEmail ?? UserEmail)%></td>
    </tr>
    <tr>
        <td>Message</td>
        <td><%= Html.TextArea("Message", Model.Message) %></td>
    </tr>
    <tr>
        <td colspan="2"><input type="submit" value="Send" style="float: right;"/></td>
    </tr>
</table>

I've been futzing around with everything I could think of. Html.TextBox works fine in other files in the same project, so I can't figure out why this is blowing chunks.

UPDATE

I've just discovered that in the project where this code works, VS recognizes the project as an MVC web application, but in the second one where it doesn't work, VS does not recognize that it's MVC. At least if I right click on a Views subfolder the former has a context menu item for 'New View', where the latter project doesn't have this.

Now all I have to do is figure out how to turn it into an MVC project, and maybe that will fix it.

UPDATE #2

Drat, that didn't work. I modified the Import directive to use System.Web.Mvc.Html, and now at least intellisense shows the definition for the .TextBox extension - will try restarting the box to see what happens. :(

final update

As I posted in the comments, I found the error and it had to do with a completely different file, so it was basically my mistake and not a code problem. :(

A: 

Are you sure Form (as in Inherits="MainWeb.Views.EmailToFriend.Form") inherits from MVC's ViewPage base class?

My guess is you either want to have

Inherits="System.Web.Mvc.ViewPage"

or

namespace MainWeb.Views.EmailToFriend
{
    public class Form : System.Web.Mvc.ViewPage
    {
    }
}

Most likely you also don't need CodeBehind="Form.ascx.cs". Did you add a regular web form instead of MVC view to your project?

Robert Wilczynski
Actually, it inherits from the MVC ViewUserControl class. The other weird thing is that in the first project where it works, the Html.TextBox lines can find the definition in the System.Web.Mvc.Html.InputExtensions class, whereas in the project that I copied the files into, it can't find the definition for TextBox. It's like it's stuck with an old version of MVC or something, but I've reset that too.
marcel_g
Is this the same machine for both working and not working project?Did you install any pre-release versions of asp.net mvc?What is the full name of the mvc assembly (referenced in web.config)
Robert Wilczynski
Those are good questions. Both projects are running on the same VM. Actually the non-working one has working references to Html.TextBox in other ViewUserControls, so it's even more confusing.
marcel_g
Does e:\pathtocode\Web\Views\EmailToFriend\ folder has any web.config in it?
Robert Wilczynski
Turns out I just needed to get the correct import directives into the correct files. I got fixated on the files that were showing errors in VS, which were different than the files actually break first at runtime. I could have solved this more quickly if I could read. So, sorry to waste everyone's time. Terribly embarrassed. um. carry on...
marcel_g
A: 

Have you made sure that the compiler is set to 3.5 in the codedom section of the web.config? I battled for hours trying to figure out why I could not use html helpers, most posts are advising to add the System.Web.Mvc.Html namespace which is neccessary for the intellisense, yet since the pages aren't compiled until they are rendered you don't realise that this is false security. Here is a post to fix the compiler version issue: http://stackoverflow.com/questions/944015/problem-creating-my-own-extension-to-htmlhelper/944761#944761

rjarmstrong