1

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;wkst=1&amp;bgcolor=%23FFAD57&amp;src=ck1tburd835alnt9rr3li68128%40group.calendar.google.com&amp;color=%23AB8B00&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>
3
  • the inline style makes me weep. Commented Sep 21, 2009 at 2:16
  • 3
    comment like that makes me weep. At least be kind enough to provide insight into alternatives. Commented Sep 21, 2009 at 2:18
  • I DO intend to move to the Sparc engine but this is already my first sharp architecture, asp mvc, and nhibernate project - don't want to throw in too many new frameworks. Commented Sep 21, 2009 at 2:21

2 Answers 2

5

Use

<%=Html.TextBox

instead of

<% Html.TextBox

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.