我们为同一家公司有两个独立的前端项目,除了所有的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子文件夹,前者有一个“New View”的上下文菜单项,后一个项目没有这个。
现在我要做的就是弄清楚如何把它变成一个MVC项目,也许这会解决它。
更新#2
Drat,那不起作用。我修改了Import指令以使用System.Web.Mvc.Html,现在至少intellisense显示.TextBox扩展名的定义 - 将尝试重新启动该框以查看发生了什么。 :(最终更新
正如我在评论中发布的那样,我发现错误并且它与一个完全不同的文件有关,所以这基本上是我的错误而不是代码问题。 :(
答案 0 :(得分:0)
你确定Form(在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视图?
答案 1 :(得分:0)
您是否确保在web.config的codedom部分中将编译器设置为3.5?我花了好几个小时试图弄清楚为什么我不能使用html助手,大多数帖子都建议添加System.Web.Mvc.Html命名空间,这对于intellisense是必要的,但由于这些页面在被渲染之前不会被编译你没有意识到这是虚假的安全。这是修复编译器版本问题的帖子:Problem creating my own extension to HtmlHelper