Partager via


InputExtensions Class

Represents support for HTML input controls in an application.

Inheritance Hierarchy

System.Object
  System.Web.Mvc.Html.InputExtensions

Namespace:  System.Web.Mvc.Html
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)

Syntax

'Declaration
<ExtensionAttribute> _
Public NotInheritable Class InputExtensions
public static class InputExtensions
[ExtensionAttribute]
public ref class InputExtensions abstract sealed

Methods

  Name Description
Public methodStatic member CheckBox(HtmlHelper, String) Returns a check box input element by using the specified HTML helper and the name of the form field.
Public methodStatic member CheckBox(HtmlHelper, String, Boolean) Returns a check box input element by using the specified HTML helper, the name of the form field, and a value to indicate whether the check box is selected.
Public methodStatic member CheckBox(HtmlHelper, String, IDictionary<String, Object>) Returns a check box input element by using the specified HTML helper, the name of the form field, and the HTML attributes.
Public methodStatic member CheckBox(HtmlHelper, String, Object) Returns a check box input element by using the specified HTML helper, the name of the form field, and the HTML attributes.
Public methodStatic member CheckBox(HtmlHelper, String, Boolean, IDictionary<String, Object>) Returns a check box input element by using the specified HTML helper, the name of the form field, a value to indicate whether the check box is selected, and the HTML attributes.
Public methodStatic member CheckBox(HtmlHelper, String, Boolean, Object) Returns a check box input element by using the specified HTML helper, the name of the form field, a value that indicates whether the check box is selected, and the HTML attributes.
Public methodStatic member CheckBoxFor<TModel>(HtmlHelper<TModel>, Expression<Func<TModel, Boolean>>) Returns a check box input element for each property in the object that is represented by the specified expression.
Public methodStatic member CheckBoxFor<TModel>(HtmlHelper<TModel>, Expression<Func<TModel, Boolean>>, IDictionary<String, Object>) Returns a check box input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.
Public methodStatic member CheckBoxFor<TModel>(HtmlHelper<TModel>, Expression<Func<TModel, Boolean>>, Object) Returns a check box input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.
Public methodStatic member Hidden(HtmlHelper, String) Returns a hidden input element by using the specified HTML helper and the name of the form field.
Public methodStatic member Hidden(HtmlHelper, String, Object) Returns a hidden input element by using the specified HTML helper, the name of the form field, and the value.
Public methodStatic member Hidden(HtmlHelper, String, Object, IDictionary<String, Object>) Returns a hidden input element by using the specified HTML helper, the name of the form field, the value, and the HTML attributes.
Public methodStatic member Hidden(HtmlHelper, String, Object, Object) Returns a hidden input element by using the specified HTML helper, the name of the form field, the value, and the HTML attributes.
Public methodStatic member HiddenFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>) Returns an HTML hidden input element for each property in the object that is represented by the specified expression.
Public methodStatic member HiddenFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, IDictionary<String, Object>) Returns an HTML hidden input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.
Public methodStatic member HiddenFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, Object) Returns an HTML hidden input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.
Public methodStatic member Password(HtmlHelper, String) Returns a password input element by using the specified HTML helper and the name of the form field.
Public methodStatic member Password(HtmlHelper, String, Object) Returns a password input element by using the specified HTML helper, the name of the form field, and the value.
Public methodStatic member Password(HtmlHelper, String, Object, IDictionary<String, Object>) Returns a password input element by using the specified HTML helper, the name of the form field, the value, and the HTML attributes.
Public methodStatic member Password(HtmlHelper, String, Object, Object) Returns a password input element by using the specified HTML helper, the name of the form field, the value, and the HTML attributes.
Public methodStatic member PasswordFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>) Returns a password input element for each property in the object that is represented by the specified expression.
Public methodStatic member PasswordFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, IDictionary<String, Object>) Returns a password input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.
Public methodStatic member PasswordFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, Object) Returns a password input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.
Public methodStatic member RadioButton(HtmlHelper, String, Object) Returns a radio button input element that is used to present mutually exclusive options.
Public methodStatic member RadioButton(HtmlHelper, String, Object, Boolean) Returns a radio button input element that is used to present mutually exclusive options.
Public methodStatic member RadioButton(HtmlHelper, String, Object, IDictionary<String, Object>) Returns a radio button input element that is used to present mutually exclusive options.
Public methodStatic member RadioButton(HtmlHelper, String, Object, Object) Returns a radio button input element that is used to present mutually exclusive options.
Public methodStatic member RadioButton(HtmlHelper, String, Object, Boolean, IDictionary<String, Object>) Returns a radio button input element that is used to present mutually exclusive options.
Public methodStatic member RadioButton(HtmlHelper, String, Object, Boolean, Object) Returns a radio button input element that is used to present mutually exclusive options.
Public methodStatic member RadioButtonFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, Object) Returns a radio button input element for each property in the object that is represented by the specified expression.
Public methodStatic member RadioButtonFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, Object, IDictionary<String, Object>) Returns a radio button input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.
Public methodStatic member RadioButtonFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, Object, Object) Returns a radio button input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.
Public methodStatic member TextBox(HtmlHelper, String) Returns a text input element by using the specified HTML helper and the name of the form field.
Public methodStatic member TextBox(HtmlHelper, String, Object) Returns a text input element by using the specified HTML helper, the name of the form field, and the value.
Public methodStatic member TextBox(HtmlHelper, String, Object, IDictionary<String, Object>) Returns a text input element by using the specified HTML helper, the name of the form field, the value, and the HTML attributes.
Public methodStatic member TextBox(HtmlHelper, String, Object, Object) Returns a text input element by using the specified HTML helper, the name of the form field, the value, and the HTML attributes.
Public methodStatic member TextBoxFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>) Returns a text input element for each property in the object that is represented by the specified expression.
Public methodStatic member TextBoxFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, IDictionary<String, Object>) Returns a text input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.
Public methodStatic member TextBoxFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, Object) Returns a text input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.

Top

Remarks

The InputExtensions class contains methods that extend the HtmlHelper class. Each extension method renders an HTML input element. With the exception of the Password extension method, each extension method can repopulate its values from the posted values by using the ModelState object. The following input element types are available in this class:

Examples

The following example shows how to use the methods of the InputExtensions class. The view displays a text box, a password box, some radio buttons, and some check boxes.

<h2><%= Html.Encode(ViewData("Message")) %></h2>
<br /><br />
<% Using Html.BeginForm("Input", "Home")%>
    Enter your name: <%= Html.TextBox("name") %>
    <br /><br />
    Enter a password: <%= Html.Password("pass") %> (Not actually used.)
    <br /><br />
    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 
    <br /><br />
    <%= Html.CheckBox("technical") %> I like to read technical books.<br />
    <%= Html.CheckBox("novels") %> I like to read novels.<br />
    <%= Html.CheckBox("comic") %> I like to read comic books.<br /> 
    <br /><br />
    <input type="submit" value="Submit" />
<% End Using%>
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<br /><br />
<% using(Html.BeginForm("Input", "Home")) %>
<% { %>
    Enter your name: <%= Html.TextBox("name") %>
    <br /><br />
    Enter a password: <%= Html.Password("pass") %> (Not actually used.)
    <br /><br />
    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 
    <br /><br />
    <%= Html.CheckBox("technical") %> I like to read technical books.<br />
    <%= Html.CheckBox("novels") %> I like to read novels.<br />
    <%= Html.CheckBox("comic") %> I like to read comic books.<br /> 
    <br /><br />
    <input type="submit" value="Submit" />
<% } %>

The following example shows a form. When the user submits the form, the Input action method processes the form data and renders a view.

Function Input(ByVal name As String, ByVal pass As String, _
               ByVal favColor As String, ByVal technical As Boolean, _
               ByVal novels As Boolean, _
               ByVal comic As Boolean) As ActionResult

    ViewData("name") = name
    ViewData("favColor") = favColor

    Dim bookMsg As String = ""
    If (technical) Then bookMsg = "You read technical books.<br />"
    If (novels) Then bookMsg = bookMsg + "You read novels.<br />"
    If (comic) Then bookMsg = bookMsg + "You read comic books."
    ViewData("books") = bookMsg

    Return View()
End Function
public ActionResult Input(string name, string pass, 
    string favColor, Boolean technical, Boolean novels, 
    Boolean comic)
{
    ViewData["name"] = name;
    ViewData["favColor"] = favColor;

    string bookMsg = "";
    if (technical) bookMsg = "You read tehnical books.<br />";
    if (novels) bookMsg += "You read novels.<br />";
    if (comic) bookMsg += "You read comic books.";
    ViewData["books"] = bookMsg;

    return View();
}

In the following example, the view displays the data that was submitted.

<h2>HTML Input Extensions</h2>

<p>Hello, <b><%=Html.Encode(ViewData("name"))%></b></p>

<p>Your favorite color: <b><%=Html.Encode(ViewData("favColor"))%></b></p>

<p><%=ViewData("books")%></p>
<h2>HTML Input Extensions</h2>

<p>Hello, <b><%= Html.Encode(ViewData["name"])%></b></p>

<p>Your favorite color: <b><%= Html.Encode(ViewData["favColor"]) %></b></p>

<p><%= ViewData["books"] %></p>

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System.Web.Mvc.Html Namespace

Other Resources

Rendering a Form in ASP.NET MVC Using HTML Helpers