Rendering a Form in asp.net MVC Using HTML Helpers

Source: Internet
Author: User
Tags html form

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

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

How to create a custom 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 you to work with the most frequently used HTML. The last section shows a example that incorporates the HTML helpers described into this topic. Available HTML Helpers

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

Actionlink-links to a action method.

BeginForm *-marks The "start of a" and links to "action method" that renders the form.

CheckBox *-renders a check box.

DropDownList *-renders a drop-down list.

Hidden-embeds information in the form this is isn't rendered for the "user to".

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 the 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, the parameters of the action D The Controller to submit the form. The BeginForm helper implements the Idisposableinterface, which enables the Using keyword (using in Visual basi c), similar to ASP.net AJAX usage.

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

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

can also use the BeginForm helper declaratively. The difference between using beginform declaratively and using an HTML form tag was that BeginForm assigns default value fo R 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 of 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 it simplest form, DropDownList takes one parameter, the name of the ViewData key whose value is of type selectlist and That's 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 is empty, the framework looks for a 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 the Index action by which values are added to a List object. The List object is passed to a 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 it simplest form, the method takes three parameters:the name to the control group, the option value, and a Boolean VA Lue 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/> <%=
(" 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 where examples. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.