1

我们为同一家公司有两个独立的前端项目,除了所有的 html 和 css 之外,它们基本相同。(同一公司内的不同部门)我正在尝试将一个内置的页面添加到另一个页面上。(是的,是的,我知道我们可能应该构建一个应用程序,根据正在运行的部门的实例显示不同的演示文稿,这样我们就不必维护两个单独但相同的代码库,但我们就是不能去有这个客户。)

无论如何,我复制了控制器、模型以及 aspx 和 ascx 页面。我需要更改的只是根名称空间上的名称。由于某种原因,在第一个项目中成功编译的特定 ascx 页面在第二个项目中失败。

这是错误消息:

    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?)

这是 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>

我一直在摆弄我能想到的一切。Html.TextBox 在同一个项目的其他文件中工作正常,所以我不知道为什么这会导致大块。

更新

我刚刚发现在此代码工作的项目中,VS 将项目识别为 MVC Web 应用程序,但在第二个它不起作用的项目中,VS 不识别它是 MVC。至少如果我右键单击 Views 子文件夹,前者有一个“新视图”的上下文菜单项,而后者的项目没有这个。

现在我要做的就是弄清楚如何把它变成一个 MVC 项目,也许这会解决它。

更新#2

德拉特,那没用。我修改了 Import 指令以使用 System.Web.Mvc.Html,现在至少 intellisense 显示了 .TextBox 扩展名的定义 - 将尝试重新启动该框以查看会发生什么。:(

最终更新

正如我在评论中发布的那样,我发现了错误并且它与一个完全不同的文件有关,所以这基本上是我的错误而不是代码问题。:(

4

2 回答 2

0

您是否确保在 web.config 的 codedom 部分中将编译器设置为 3.5?我挣扎了好几个小时试图弄清楚为什么我不能使用 html 帮助程序,大多数帖子都建议添加智能感知所必需的 System.Web.Mvc.Html 命名空间,但由于页面在渲染之前不会被编译您没有意识到这是错误的安全性。这是修复编译器版本问题的帖子:Problem creating my own extension to HtmlHelper

于 2009-12-02T11:22:54.920 回答
0

您确定 Form (as in Inherits="MainWeb.Views.EmailToFriend.Form") 继承自 MVC 的 ViewPage 基类吗?

我的猜测是你要么想要

Inherits="System.Web.Mvc.ViewPage"

或者

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

很可能您也不需要CodeBehind="Form.ascx.cs". 您是否在项目中添加了常规 Web 表单而不是 MVC 视图?

于 2009-07-08T16:27:36.457 回答