LA.NET [EN]

Dec 26

In the lasts posts, we’ve started looking at some of the HtmlHelper extension methods that you can use to render controls on your forms (we’ve looked at several methods you can use to render HTML INPUT controls). Today we’re going to keep looking  at the extenders, but we’ll concentrate on how to generate forms on your views.

The FormExtensions.cs file contains several extension methods that you can use to create forms. You’ll find several overloads of the BeginForm and EndForm methods. The BeginForm method lets you set up the attributes you’d normally apply to an HTML form (ie, action and method). There are overloads of this method that will let you fill those attributes automatically or you can use any of the overloads that lets you specify the value of each of those attributes. As you might expect, some of the overloads let you pass the name of the controller/action for specifying the action (ie, the url from where your form will post its data).

The BeginForm methods render the <form> begin tag into the response stream and returns an instance of type MvcForm. This simple class implements the IDisposable method. Its Dispose method implementation is responsible for closing the end form tag. So, you could generate a form by writing something like this (I’ll be rewriting the form I’ve presented in one of my previous examples):

<% using( var frm = Html.BeginForm("HandleFormData", "Home")) {%>
<label for="name">Name:</label><%=Html.TextBox("name")%>
  <%=Html.ValidationMessage("name", "*")%>
  <br />
  <label for="Password">Password:</label><%=Html.Password("Password")%>
  <%=Html.ValidationMessage("name", "*")%>
  <br />
  <label for="male">Male:</label>
  <%=Html.RadioButton("sex", "male", new {id = "male"})%>
  <label for="female">Female:</label>
  <%=Html.RadioButton("sex","female",new {id = "female"})%>
  <input type="submit" id="myBt" value="Try to submit" /> <br />
  <%=Html.ValidationSummary()%>
<%}%>

If you don’t really enjoy the using syntax, then you can use the BeginForm/EndForm pair:

<%Html.BeginForm("HandleFormData", "Home"); %>
     <label for="name">Name:</label><%=Html.TextBox("name")%>
      <%=Html.ValidationMessage("name", "*")%>
      <br />
      <label for="Password">Password:</label><%=Html.Password("Password")%>
      <%=Html.ValidationMessage("name", "*")%>
      <br />
      <label for="male">Male:</label>
      <%=Html.RadioButton("sex", "male", new {id = "male"})%>
      <label for="female">Female:</label>
      <%=Html.RadioButton("sex", "female", new {id = "female"})%>
      <input type="submit" id="myBt" value="Try to submit" /> <br />
      <%=Html.ValidationSummary()%>
<%Html.EndForm();%>

Notice that since the BeginForm doesn’t return a string, you must not use the Response.Write “shortcut” (ie, <%= %>) and you must end up the statement with a ;. So, now you’ve got at least three ways of generating a form:

  • you can use the classic approach, where you write the HTML directly (as we’ve seen in the oldest examples);
  • you can use the disposable syntax (based on the using helper);
  • you can use the BeginForm/EndForm pair of methods.

Any of these options works well, so choose the one you prefer (in most of the examples I’ve built, I did use the first approach, where I explicitly write the FORM tag). On the next post, we’ll keep up looking at the HtmlHelper extension methods. Keep tuned!

1 comment so far

  1. nkwefjn
    7:13 am - 12-20-2009

    Zq1Idg wkdghcxketdf, [url=http://kkfzgwmpqpza.com/]kkfzgwmpqpza[/url], [link=http://jjpbpqbwgkjk.com/]jjpbpqbwgkjk[/link], http://pfutnsiaplqa.com/