views:

131

answers:

2

Below is my asp mvc view. Note that it has a div which contains a simple form. I am using Html.TextBox() to try to output input elements but nothing is output. The form renders properly but where I expect to see the input tag there is nothing.

I'm sure this is a total beginner mistake but what am I doing wrong?

<%@ 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;"> &nbsp; </div>
    <div id="calendarContainer" class="container" style="width: 500px; height: 482px; float: left;"> &nbsp;
        <iframe src="http://www.google.com/calendar/embed?height=462&amp;amp;wkst=1&amp;amp;bgcolor=%23FFAD57&amp;amp;src=ck1tburd835alnt9rr3li68128%40group.calendar.google.com&amp;amp;color=%23AB8B00&amp;amp;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>
+5  A: 

Use

<%=Html.TextBox

instead of

<% Html.TextBox

<%= is equivalent to Response.Write, whereas <% merely opens a code block.

spender
+1  A: 

It is important to check the return types of the HtmlHelper methods. Some, such as RenderPartial, return void. These internally used Response.Write() or another method to output some HTML directly to the response steream.

Thus, they can be used in an ASP code block, which executes any inline code, like so:

<% Html.RenderPartial("SubsciberProfile") %>

However, most of the built-in form methods, such as Html.TextBox, return a string. In these cases, you must execute the code and send it to the response. If you used

<% Html.TextBox("subscriberEmail") %>

then the TextBox HTML would be returned as a string and promptly discarded. It is the equivalent of doing something like this:

string name = "John Doe";
name.Replace("Doe","Smith");

Note that the value returned by Replace is never assigned to anything, so the evaluation of the method is performed but its return value never used.

Instead, we must use something like this:

<%= Html.TextBox("subscriberEmail") %>

Note the equals sign! This means the code block should evaluate and output the result of the method. As pointed out above, <%= someString %> is shorthand for <% Response.Write(someString) %>. This is subtle, but very important to remember.

JoshJordan