Rendering a Form in ASP.NET MVC Using HTML Helpers

來源:互聯網
上載者:User

轉自:http://msdn.microsoft.com/en-us/library/dd410596(v=vs.100).aspx

更多:http://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper(v=vs.118).aspx

如何建立自訂html helper:http://www.asp.net/mvc/tutorials/older-versions/views/creating-custom-html-helpers-cs

The ASP.NET MVC framework includes helper methods that provide an easy way to render HTML in a view. This topic explains how to work with the most frequently used HTML helpers. The last section shows an example that incorporates the HTML helpers described in this topic. Available HTML Helpers

The following list shows some of the currently available HTML helpers. The helpers listed with an asterisk (*) are demonstrated in this topic.

ActionLink — Links to an action method.

BeginForm * — Marks the start of a form and links to the action method that renders the form.

CheckBox * — Renders a check box.

DropDownList * — Renders a drop-down list.

Hidden — Embeds information in the form that is not rendered for the user to see.

ListBox — Renders a list box.

Password — Renders a text box for entering a password.

RadioButton * — Renders a radio button.

TextArea — Renders a text area (multi-line text box).

TextBox * — Renders a text box. Using the BeginForm Helper

The BeginForm helper marks the start of an HTML form and renders as an HTML form element. The BeginForm helper method has several overrides. The version of the BeginForm helper shown in the following example takes two parameters, the names of the action method and the controller to submit the form. The BeginForm helper implements the IDisposableinterface, which enables you to use the using keyword (Using in Visual Basic), similar to ASP.NET AJAX usage.

The following example shows how to use the BeginForm helper in a using pattern. C# VB

<% using(Html.BeginForm("HandleForm", "Home")) %><% { %>    <!-- Form content goes here --><% } %>

You can also use the BeginForm helper declaratively. The difference between using BeginForm declaratively and using an HTML form tag is that BeginForm assigns default value for the action method and action attributes, which simplifies the markup. The following example uses declarative markup to mark the start and ending of a form. C# VB

<% Html.BeginForm(); %>    <!-- Form content goes here --><% Html.EndForm(); %>
Using the CheckBox Helper

The CheckBox helper method renders a check box that has the name that you specify. The rendered control returns a Boolean value (true or false). The following example shows markup for the CheckBox helper method.

<%= Html.CheckBox("bookType") %>
Using the DropDownList Helper

The DropDownList helper renders a drop-down list. In its simplest form, DropDownList takes one parameter, the name of the ViewData key whose value is of type SelectList and that contains the option values for the drop-down list. The MVC framework uses the ModelState property of ViewData to determine the selected value. If the ModelState property is empty, the framework looks for an item whose Selected property is set.

The following example shows markup for the DropDownList helper method.

<%= Html.DropDownList("pets") %>
Note

Both the DropDownList and ListBox helpers accept either a SelectList or MultiSelectList object.

The following code is a part of an Index action method in which values are added to a List object. The List object is passed to an instance of SelectList, which is then added to the ViewData object. C# VB

List<string> petList = new List<string>();petList.Add("Dog");petList.Add("Cat");petList.Add("Hamster");petList.Add("Parrot");petList.Add("Gold fish");petList.Add("Mountain lion");petList.Add("Elephant");ViewData["Pets"] = new SelectList(petList);
Using the RadioButton Helper

The RadioButton helper method renders a radio button. In its simplest form, the method takes three parameters: the name of the control group, the option value, and a Boolean value that determines whether the radio button is selected initially. The following markup shows markup for the RadioButton helper method.

Select your favorite color:<br /><%= Html.RadioButton("favColor", "Blue", true) %> Blue <br /><%= Html.RadioButton("favColor", "Purple", false)%> Purple <br /><%= Html.RadioButton("favColor", "Red", false)%> Red <br /><%= Html.RadioButton("favColor", "Orange", false)%> Orange <br /><%= Html.RadioButton("favColor", "Yellow", false)%> Yellow <br /><%= Html.RadioButton("favColor", "Brown", false)%> Brown <br /><%= Html.RadioButton("favColor", "Green", false)%> Green
Using the TextBox Helper

The TextBox helper method renders a text box that has the specified name. The following markup shows markup for theTextBox helper method.

Enter your name: <%= Html.TextBox("name") %>
Example Application

The following example is a complete example from which the previous examples where taken. The Index page displays a form that implements HTML helper methods. When the user submits the form, the form is handled by the HandleForm action method, which generates a view that displays the information that the user submitted.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.