Class HtmlHelper
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
viewContextViewContextThe view context.
viewDataContainerIViewDataContainerThe 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
viewContextViewContextThe view context.
viewDataContainerIViewDataContainerThe view data container.
routeCollectionRouteCollectionThe 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
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
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
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
ValidationSummaryCssClassName
The name of the CSS class that is used to style validation summary error messages.
public static readonly string ValidationSummaryCssClassName
Field Value
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
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
ValidationSummaryMessageElement
Element name used to wrap a top-level message in ValidationSummary(HtmlHelper) and other overloads.
public static string ValidationSummaryMessageElement { get; set; }
Property Value
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
htmlAttributesobjectThe 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
valueobjectThe 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
valuestringThe 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
enabledbooltrue 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
enabledbooltrue 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
valueobjectThe 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
valuestringThe string to encode.
Returns
- string
The HTML-encoded string.
FormatValue(object, string)
Formats the value.
public string FormatValue(object value, string format)
Parameters
Returns
- string
The formatted value.
GenerateIdFromName(string)
Creates an HTML element ID using the specified element name.
public static string GenerateIdFromName(string name)
Parameters
namestringThe 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
namestringThe name of the HTML element.
idAttributeDotReplacementstringThe 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.
GenerateLink(RequestContext, RouteCollection, string, string, string, string, string, string, string, RouteValueDictionary, IDictionary<string, object>)
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
requestContextRequestContextThe context of the HTTP request.
routeCollectionRouteCollectionThe collection of URL routes.
linkTextstringThe text caption to display for the link.
routeNamestringThe name of the route that is used to return a virtual path.
actionNamestringThe name of the action method.
controllerNamestringThe name of the controller.
protocolstringThe communication protocol, such as HTTP or HTTPS. If this parameter is null, the protocol defaults to HTTP.
hostNamestringThe name of the host.
fragmentstringThe fragment identifier.
routeValuesRouteValueDictionaryAn object that contains the parameters for a route.
htmlAttributesIDictionary<string, object>An object that contains the HTML attributes for the element.
Returns
- string
An HTML element that links to the specified action method.
GenerateLink(RequestContext, RouteCollection, string, string, string, string, RouteValueDictionary, IDictionary<string, object>)
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
requestContextRequestContextThe context of the HTTP request.
routeCollectionRouteCollectionThe collection of URL routes.
linkTextstringThe text caption to display for the link.
routeNamestringThe name of the route that is used to return a virtual path.
actionNamestringThe name of the action method.
controllerNamestringThe name of the controller.
routeValuesRouteValueDictionaryAn object that contains the parameters for a route.
htmlAttributesIDictionary<string, object>An object that contains the HTML attributes for the element.
Returns
- string
An HTML element that links to the specified action method.
GenerateRouteLink(RequestContext, RouteCollection, string, string, string, string, string, RouteValueDictionary, IDictionary<string, object>)
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
requestContextRequestContextThe context of the HTTP request.
routeCollectionRouteCollectionThe collection of URL routes.
linkTextstringThe text caption to display for the link.
routeNamestringThe name of the route that is used to return a virtual path.
protocolstringThe communication protocol, such as HTTP or HTTPS. If this parameter is null, the protocol defaults to HTTP.
hostNamestringThe name of the host.
fragmentstringThe fragment identifier.
routeValuesRouteValueDictionaryAn object that contains the parameters for a route.
htmlAttributesIDictionary<string, object>An object that contains the HTML attributes for the element.
Returns
- string
An HTML element that links to the specified URL route.
GenerateRouteLink(RequestContext, RouteCollection, string, string, RouteValueDictionary, IDictionary<string, object>)
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
requestContextRequestContextThe context of the HTTP request.
routeCollectionRouteCollectionThe collection of URL routes.
linkTextstringThe text caption to display for the link.
routeNamestringThe name of the route that is used to return a virtual path.
routeValuesRouteValueDictionaryAn object that contains the parameters for a route.
htmlAttributesIDictionary<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
methodFormMethodThe 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
inputTypeInputTypeThe 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
namestringThe 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
namestringThe HTML name attribute.
metadataModelMetadataThe 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
httpMethodstringThe 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
httpVerbHttpVerbsThe 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
valueobjectThe 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
valueobjectThe HTML markup.
Returns
- IHtmlString
The HTML markup without encoding.
Raw(string)
Returns markup that is not HTML encoded.
public IHtmlString Raw(string value)
Parameters
valuestringThe 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
elementNamestring
SetValidationSummaryMessageElement(string)
Set element name used to wrap a top-level message in ValidationSummary(HtmlHelper) and other overloads.
public void SetValidationSummaryMessageElement(string elementName)
Parameters
elementNamestring