Writing html helper - travel-kmv.ru

An Absolute Beginner s Tutorial on html Helpers and Creating

Imagine, for example, that you decide to create a new html Helper that renders an html label tag. You can use the class in Listing 2 to render a label. Listing 2 using System; namespace MvcApplication1.Helpers public class LabelHelper public static string Label(string target, string text) return rmat label for'0' 1 /label target, text There is nothing special about the. TextBox lastName br / br / input type"submit" value"Register" / 1 /div /body /html The ginForm Helper method is used to create the opening and closing html form tags. Notice that the ginForm method is called within a using statement.

Listing 2 px @ Page Language"C AutoEventWireup"true" CodeBehind" Inherits"dex2" @ Import Namespace"MvcApplication1.Helpers"!doctype html public "-/W3C/DTD xhtml 1.0 Transitional/EN" "http www. W3.org/TR/xhtml1/DTD/xhtml1-transitional. Dtd" html xmlns"http www. W3.org/1999/xhtml" head id"Head1" runat"server" title Index2 /title /head body div using (ginForm bel firstName "First Name br / Html. W3.org/TR/xhtml1/DTD/xhtml1-transitional. Dtd" html xmlns "http www. W3.org/1999/xhtml " head id"Head1" runat"server" title Index /title /head body div using (ginForm label for"firstName" First Name: /label br / Html. TextBox firstName br / br / label for"lastName" Last Name: /label br / Html.

The using statement ensures that the form tag gets closed at the end of the using block. If you prefer, instead of creating a using block, you can call the Html.

TextBox firstName br / br / bel lastName "Last Name br / Html. TextBox lastName br / br / input type"submit" value"Register" / /div /body /html Creating html Helpers with Extension Methods If you want to create html Helpers that work just like the standard html Helpers included in the T MVC framework then you need to create extension methods. In the first part of this tutorial, I describe some of the existing html Helpers included with the T MVC framework. Next, I describe two methods of creating custom html Helpers: I explain how to create custom html Helpers by creating a static method and by creating an extension method. If you select view source in your browser then you see the html source in Listing 2. Notice that the source contains standard html tags. Important: notice that the Html. TextBox -html Helper is rendered with tags instead of tags.