Table of Contents

Class HtmlHelper

Namespace
System.Web.Mvc
Assembly
System.Web.Mvc.dll

Supports the rendering of HTML controls in a view.

public class HtmlHelper
Inheritance
HtmlHelper
Derived
Inherited Members
Extension Methods

Constructors

HtmlHelper(ViewContext, IViewDataContainer)

Initializes a new instance of the HtmlHelper class by using the specified view context and view data container.

public HtmlHelper(ViewContext viewContext, IViewDataContainer viewDataContainer)

Parameters

viewContext ViewContext

The view context.

viewDataContainer IViewDataContainer

The view data container.

Exceptions

ArgumentNullException

The ViewContext or the viewDataContainer parameter is null.

HtmlHelper(ViewContext, IViewDataContainer, RouteCollection)

Initializes a new instance of the HtmlHelper class by using the specified view context, view data container, and route collection.

public HtmlHelper(ViewContext viewContext, IViewDataContainer viewDataContainer, RouteCollection routeCollection)

Parameters

viewContext ViewContext

The view context.

viewDataContainer IViewDataContainer

The view data container.

routeCollection RouteCollection

The route collection.

Exceptions

ArgumentNullException

One or more parameters is null.

Fields

ValidationInputCssClassName

The name of the CSS class that is used to style an input field when a validation error occurs.

public static readonly string ValidationInputCssClassName

Field Value

string

ValidationInputValidCssClassName

The name of the CSS class that is used to style an input field when the input is valid.

public static readonly string ValidationInputValidCssClassName

Field Value

string

ValidationMessageCssClassName

The name of the CSS class that is used to style the error message when a validation error occurs.

public static readonly string ValidationMessageCssClassName

Field Value

string

ValidationMessageValidCssClassName

The name of the CSS class that is used to style the validation message when the input is valid.

public static readonly string ValidationMessageValidCssClassName

Field Value

string

ValidationSummaryCssClassName

The name of the CSS class that is used to style validation summary error messages.

public static readonly string ValidationSummaryCssClassName

Field Value

string

ValidationSummaryValidCssClassName

The name of the CSS class that is used to style the validation summary when the input is valid.

public static readonly string ValidationSummaryValidCssClassName

Field Value

string

Properties

ClientValidationEnabled

Gets or sets a value that indicates whether client validation is enabled.

public static bool ClientValidationEnabled { get; set; }

Property Value

bool

true if enable client validation is enabled; otherwise, false.

Html5DateRenderingMode

Gets or sets the HTML5 date rendering mode.

public Html5DateRenderingMode Html5DateRenderingMode { get; set; }

Property Value

Html5DateRenderingMode

The HTML5 date rendering mode.

IdAttributeDotReplacement

Gets or sets the character that replaces periods in the ID attribute of an element.

public static string IdAttributeDotReplacement { get; set; }

Property Value

string

The character that replaces periods in the ID attribute of an element.

RouteCollection

Gets or sets the collection of routes for the application.

public RouteCollection RouteCollection { get; }

Property Value

RouteCollection

The collection of routes for the application.

UnobtrusiveJavaScriptEnabled

Gets or sets a value that indicates whether unobtrusive JavaScript is enabled.

public static bool UnobtrusiveJavaScriptEnabled { get; set; }

Property Value

bool

true if unobtrusive JavaScript is enabled; otherwise, false.

ValidationMessageElement

Element name used to wrap the validation message generated by ValidationMessage(HtmlHelper, string) and other overloads.

public static string ValidationMessageElement { get; set; }

Property Value

string

ValidationSummaryMessageElement

Element name used to wrap a top-level message in ValidationSummary(HtmlHelper) and other overloads.

public static string ValidationSummaryMessageElement { get; set; }

Property Value

string

ViewBag

Gets the view bag.

public dynamic ViewBag { get; }

Property Value

dynamic

The view bag.

ViewContext

Gets or sets the context information about the view.

public ViewContext ViewContext { get; }

Property Value

ViewContext

The context of the view.

ViewData

Gets the current view data dictionary.

public ViewDataDictionary ViewData { get; }

Property Value

ViewDataDictionary

The view data dictionary.

ViewDataContainer

Gets or sets the view data container.

public IViewDataContainer ViewDataContainer { get; }

Property Value

IViewDataContainer

The view data container.

Methods

AnonymousObjectToHtmlAttributes(object)

Replaces underscore characters (_) with hyphens (-) in the specified HTML attributes.

public static RouteValueDictionary AnonymousObjectToHtmlAttributes(object htmlAttributes)

Parameters

htmlAttributes object

The HTML attributes.

Returns

RouteValueDictionary

The HTML attributes with underscore characters replaced by hyphens.

AntiForgeryToken()

Generates a hidden form field (anti-forgery token) that is validated when the form is submitted.

public MvcHtmlString AntiForgeryToken()

Returns

MvcHtmlString

The generated form field (anti-forgery token).

AttributeEncode(object)

Converts the specified attribute value to an HTML-encoded string.

public string AttributeEncode(object value)

Parameters

value object

The object to encode.

Returns

string

The HTML-encoded string. If the value parameter is null or empty, this method returns an empty string.

AttributeEncode(string)

Converts the specified attribute value to an HTML-encoded string.

public string AttributeEncode(string value)

Parameters

value string

The string to encode.

Returns

string

The HTML-encoded string. If the value parameter is null or empty, this method returns an empty string.

EnableClientValidation()

Enables input validation that is performed by using client script in the browser.

public void EnableClientValidation()

EnableClientValidation(bool)

Enables or disables client validation.

public void EnableClientValidation(bool enabled)

Parameters

enabled bool

true to enable client validation; otherwise, false.

EnableUnobtrusiveJavaScript()

Enables or disables unobtrusive JavaScript.

public void EnableUnobtrusiveJavaScript()

EnableUnobtrusiveJavaScript(bool)

Enables or disables unobtrusive JavaScript.

public void EnableUnobtrusiveJavaScript(bool enabled)

Parameters

enabled bool

true to enable unobtrusive JavaScript; otherwise, false.

Encode(object)

Converts the value of the specified object to an HTML-encoded string.

public string Encode(object value)

Parameters

value object

The object to encode.

Returns

string

The HTML-encoded string.

Encode(string)

Converts the specified string to an HTML-encoded string.

public string Encode(string value)

Parameters

value string

The string to encode.

Returns

string

The HTML-encoded string.

FormatValue(object, string)

Formats the value.

public string FormatValue(object value, string format)

Parameters

value object

The value.

format string

The format string.

Returns

string

The formatted value.

GenerateIdFromName(string)

Creates an HTML element ID using the specified element name.

public static string GenerateIdFromName(string name)

Parameters

name string

The name of the HTML element.

Returns

string

The ID of the HTML element.

Exceptions

ArgumentNullException

The name parameter is null.

GenerateIdFromName(string, string)

Creates an HTML element ID using the specified element name and a string that replaces dots in the name.

public static string GenerateIdFromName(string name, string idAttributeDotReplacement)

Parameters

name string

The name of the HTML element.

idAttributeDotReplacement string

The string that replaces dots (.) in the name parameter.

Returns

string

The ID of the HTML element.

Exceptions

ArgumentNullException

The name parameter or the idAttributeDotReplacement parameter is null.

Generates an HTML anchor element (a element) that links to the specified action method, and enables the user to specify the communication protocol, name of the host, and a URL fragment.

public static string GenerateLink(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)

Parameters

requestContext RequestContext

The context of the HTTP request.

routeCollection RouteCollection

The collection of URL routes.

linkText string

The text caption to display for the link.

routeName string

The name of the route that is used to return a virtual path.

actionName string

The name of the action method.

controllerName string

The name of the controller.

protocol string

The communication protocol, such as HTTP or HTTPS. If this parameter is null, the protocol defaults to HTTP.

hostName string

The name of the host.

fragment string

The fragment identifier.

routeValues RouteValueDictionary

An object that contains the parameters for a route.

htmlAttributes IDictionary<string, object>

An object that contains the HTML attributes for the element.

Returns

string

An HTML element that links to the specified action method.

Generates an HTML anchor element (a element) that links to the specified action method.

public static string GenerateLink(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)

Parameters

requestContext RequestContext

The context of the HTTP request.

routeCollection RouteCollection

The collection of URL routes.

linkText string

The text caption to display for the link.

routeName string

The name of the route that is used to return a virtual path.

actionName string

The name of the action method.

controllerName string

The name of the controller.

routeValues RouteValueDictionary

An object that contains the parameters for a route.

htmlAttributes IDictionary<string, object>

An object that contains the HTML attributes for the element.

Returns

string

An HTML element that links to the specified action method.

Generates an HTML anchor element (a element) that links to the specified URL route, and enables the user to specify the communication protocol, name of the host, and a URL fragment.

public static string GenerateRouteLink(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)

Parameters

requestContext RequestContext

The context of the HTTP request.

routeCollection RouteCollection

The collection of URL routes.

linkText string

The text caption to display for the link.

routeName string

The name of the route that is used to return a virtual path.

protocol string

The communication protocol, such as HTTP or HTTPS. If this parameter is null, the protocol defaults to HTTP.

hostName string

The name of the host.

fragment string

The fragment identifier.

routeValues RouteValueDictionary

An object that contains the parameters for a route.

htmlAttributes IDictionary<string, object>

An object that contains the HTML attributes for the element.

Returns

string

An HTML element that links to the specified URL route.

Generates an HTML anchor element (a element) that links to the specified URL route.

public static string GenerateRouteLink(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)

Parameters

requestContext RequestContext

The context of the HTTP request.

routeCollection RouteCollection

The collection of URL routes.

linkText string

The text caption to display for the link.

routeName string

The name of the route that is used to return a virtual path.

routeValues RouteValueDictionary

An object that contains the parameters for a route.

htmlAttributes IDictionary<string, object>

An object that contains the HTML attributes for the element.

Returns

string

An HTML element that links to the specified URL route.

GetFormMethodString(FormMethod)

Returns the HTTP method that handles form input (GET or POST) as a string.

public static string GetFormMethodString(FormMethod method)

Parameters

method FormMethod

The HTTP method that handles the form.

Returns

string

The form method string, either "get" or "post".

GetInputTypeString(InputType)

Returns the HTML input control type as a string.

public static string GetInputTypeString(InputType inputType)

Parameters

inputType InputType

The enumerated input type.

Returns

string

The input type string ("checkbox", "hidden", "password", "radio", or "text").

GetUnobtrusiveValidationAttributes(string)

Gets the collection of unobtrusive JavaScript validation attributes using the specified HTML name attribute.

public IDictionary<string, object> GetUnobtrusiveValidationAttributes(string name)

Parameters

name string

The HTML name attribute.

Returns

IDictionary<string, object>

The collection of unobtrusive JavaScript validation attributes.

GetUnobtrusiveValidationAttributes(string, ModelMetadata)

Gets the collection of unobtrusive JavaScript validation attributes using the specified HTML name attribute and model metadata.

public IDictionary<string, object> GetUnobtrusiveValidationAttributes(string name, ModelMetadata metadata)

Parameters

name string

The HTML name attribute.

metadata ModelMetadata

The model metadata.

Returns

IDictionary<string, object>

The collection of unobtrusive JavaScript validation attributes.

HttpMethodOverride(string)

Returns a hidden input element that identifies the override method for the specified HTTP data-transfer method that was used by the client.

public MvcHtmlString HttpMethodOverride(string httpMethod)

Parameters

httpMethod string

The HTTP data-transfer method that was used by the client (DELETE, HEAD, or PUT).

Returns

MvcHtmlString

The override method that uses the HTTP data-transfer method that was used by the client.

Exceptions

ArgumentNullException

The httpVerb parameter is not "PUT", "DELETE", or "HEAD".

HttpMethodOverride(HttpVerbs)

Returns a hidden input element that identifies the override method for the specified verb that represents the HTTP data-transfer method used by the client.

public MvcHtmlString HttpMethodOverride(HttpVerbs httpVerb)

Parameters

httpVerb HttpVerbs

The verb that represents the HTTP data-transfer method used by the client.

Returns

MvcHtmlString

The override method that uses the verb that represents the HTTP data-transfer method used by the client.

Exceptions

ArgumentNullException

The httpVerb parameter is not "PUT", "DELETE", or "HEAD".

ObjectToDictionary(object)

Creates a dictionary from an object, by adding each public instance property as a key with its associated value to the dictionary. It will expose public properties from derived types as well. This is typically used with objects of an anonymous type.

public static IDictionary<string, object> ObjectToDictionary(object value)

Parameters

value object

The object to be converted.

Returns

IDictionary<string, object>

The created dictionary of property names and property values.

Raw(object)

Returns markup that is not HTML encoded.

public IHtmlString Raw(object value)

Parameters

value object

The HTML markup.

Returns

IHtmlString

The HTML markup without encoding.

Raw(string)

Returns markup that is not HTML encoded.

public IHtmlString Raw(string value)

Parameters

value string

The HTML markup.

Returns

IHtmlString

The HTML markup without encoding.

SetValidationMessageElement(string)

Set element name used to wrap the validation message generated by ValidationMessage(HtmlHelper, string) and other overloads.

public void SetValidationMessageElement(string elementName)

Parameters

elementName string

SetValidationSummaryMessageElement(string)

Set element name used to wrap a top-level message in ValidationSummary(HtmlHelper) and other overloads.

public void SetValidationSummaryMessageElement(string elementName)

Parameters

elementName string