下面是我的asp mvc视图。请注意,它有一个包含简单表单的div。我使用Html.TextBox()来尝试输出输入元素但没有输出。表单呈现正确但我希望看到输入标记没有任何内容。
我确定这是一个初学者的错误,但我做错了什么?
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true"
Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="gnodotnet.Web.Controllers" %>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
<div id="sponsorsContainer" class="container" style="width: 110px; float: left; height:482px; margin-right: 20px;"> </div>
<div id="calendarContainer" class="container" style="width: 500px; height: 482px; float: left;">
<iframe src="http://www.google.com/calendar/embed?height=462&wkst=1&bgcolor=%23FFAD57&src=ck1tburd835alnt9rr3li68128%40group.calendar.google.com&color=%23AB8B00&ctz=America%2FChicago" style=" border-width:0 " width="482" height="462" frameborder="0" scrolling="no"></iframe>
</div>
<div id="mailingListContainer" class="container" style="width: 95px; float: left; height:182px; margin-left: 20px;">
<% using (Html.BeginForm()) { %>
<%= Html.AntiForgeryToken() %>
<h4>Subscribe to our Mailing List</h4>
Name: <% Html.TextBox("subscribeName"); %>
Email: <% Html.TextBox("subscribeEmail"); %>
<% Html.Button("subcribeOk", "Subscribe", HtmlButtonType.Submit); %>
<% } %>
</div>
</asp:Content>
答案 0 :(得分:5)
使用
<%=Html.TextBox
而不是
<% Html.TextBox
&lt;%=等同于Response.Write,而&lt;%只是打开一个代码块。
答案 1 :(得分:1)
检查HtmlHelper
方法的返回类型非常重要。某些内容(例如RenderPartial
)会返回void
。这些内部使用的Response.Write()
或其他方法将一些HTML直接输出到响应流。
因此,它们可以在ASP代码块中使用,它执行任何内联代码,如下所示:
<% Html.RenderPartial("SubsciberProfile") %>
但是,大多数内置表单方法(例如Html.TextBox
)都会返回string
。在这些情况下,您必须执行代码并将其发送到响应。如果您使用
<% Html.TextBox("subscriberEmail") %>
然后TextBox HTML将作为string
返回并立即丢弃。这相当于做这样的事情:
string name = "John Doe";
name.Replace("Doe","Smith");
请注意,Replace
返回的值永远不会分配给任何内容,因此会执行方法的评估,但其返回值从未使用过。
相反,我们必须使用这样的东西:
<%= Html.TextBox("subscriberEmail") %>
请注意等号!这意味着代码块应该评估并输出方法的结果。如上所述,<%= someString %>
是<% Response.Write(someString) %>
的简写。这很微妙,但要记住非常重要。