Compartilhar via


AjaxHelper Class

Represents support for rendering HTML in AJAX scenarios within a view.

Inheritance Hierarchy

System.Object
  System.Web.Mvc.AjaxHelper
    System.Web.Mvc.AjaxHelper<TModel>

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

Syntax

'Declaration
Public Class AjaxHelper
public class AjaxHelper
public ref class AjaxHelper

The AjaxHelper type exposes the following members.

Constructors

  Name Description
Public method AjaxHelper(ViewContext, IViewDataContainer) Initializes a new instance of the AjaxHelper class using the specified view context and view data container.
Public method AjaxHelper(ViewContext, IViewDataContainer, RouteCollection) Initializes a new instance of the AjaxHelper class by using the specified view context, view data container, and route collection.

Top

Properties

  Name Description
Public propertyStatic member GlobalizationScriptPath Gets or sets the root path for the location to use for globalization script files.
Public property RouteCollection Gets the collection of URL routes for the application.
Public property ViewContext Gets the context information about the view.
Public property ViewData Gets the current view data dictionary.
Public property ViewDataContainer Gets the view data container.

Top

Methods

  Name Description
Public method Equals Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method JavaScriptStringEncode Serializes the specified message and returns the resulting JSON-formatted string.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Extension Methods

  Name Description
Public Extension Method ActionLink(String, String, AjaxOptions) Overloaded. Returns an anchor element that contains the URL to the specified action method; when the action link is clicked, the action method is invoked asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method ActionLink(String, String, Object, AjaxOptions) Overloaded. Returns an anchor element that contains the URL to the specified action method; when the action link is clicked, the action method is invoked asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method ActionLink(String, String, RouteValueDictionary, AjaxOptions) Overloaded. Returns an anchor element that contains the URL to the specified action method; when the action link is clicked, the action method is invoked asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method ActionLink(String, String, String, AjaxOptions) Overloaded. Returns an anchor element that contains the URL to the specified action method; when the action link is clicked, the action method is invoked asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method ActionLink(String, String, Object, AjaxOptions, Object) Overloaded. Returns an anchor element that contains the URL to the specified action method; when the action link is clicked, the action method is invoked asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method ActionLink(String, String, RouteValueDictionary, AjaxOptions, IDictionary<String, Object>) Overloaded. Returns an anchor element that contains the URL to the specified action method; when the action link is clicked, the action method is invoked asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method ActionLink(String, String, String, Object, AjaxOptions) Overloaded. Returns an anchor element that contains the URL to the specified action method; when the action link is clicked, the action method is invoked asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method ActionLink(String, String, String, RouteValueDictionary, AjaxOptions) Overloaded. Returns an anchor element that contains the URL to the specified action method; when the action link is clicked, the action method is invoked asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method ActionLink(String, String, String, Object, AjaxOptions, Object) Overloaded. Returns an anchor element that contains the URL to the specified action method; when the action link is clicked, the action method is invoked asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method ActionLink(String, String, String, RouteValueDictionary, AjaxOptions, IDictionary<String, Object>) Overloaded. Returns an anchor element that contains the URL to the specified action method; when the action link is clicked, the action method is invoked asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method ActionLink(String, String, String, String, String, String, Object, AjaxOptions, Object) Overloaded. Returns an anchor element that contains the URL to the specified action method; when the action link is clicked, the action method is invoked asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method ActionLink(String, String, String, String, String, String, RouteValueDictionary, AjaxOptions, IDictionary<String, Object>) Overloaded. Returns an anchor element that contains the URL to the specified action method; when the action link is clicked, the action method is invoked asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method BeginForm(AjaxOptions) Overloaded. Writes an opening <form> tag to the response. (Defined by AjaxExtensions.)
Public Extension Method BeginForm(String, AjaxOptions) Overloaded. Writes an opening <form> tag to the response. (Defined by AjaxExtensions.)
Public Extension Method BeginForm(String, Object, AjaxOptions) Overloaded. Writes an opening <form> tag to the response. (Defined by AjaxExtensions.)
Public Extension Method BeginForm(String, RouteValueDictionary, AjaxOptions) Overloaded. Writes an opening <form> tag to the response. (Defined by AjaxExtensions.)
Public Extension Method BeginForm(String, String, AjaxOptions) Overloaded. Writes an opening <form> tag to the response. (Defined by AjaxExtensions.)
Public Extension Method BeginForm(String, Object, AjaxOptions, Object) Overloaded. Writes an opening <form> tag to the response. (Defined by AjaxExtensions.)
Public Extension Method BeginForm(String, RouteValueDictionary, AjaxOptions, IDictionary<String, Object>) Overloaded. Writes an opening <form> tag to the response. (Defined by AjaxExtensions.)
Public Extension Method BeginForm(String, String, Object, AjaxOptions) Overloaded. Writes an opening <form> tag to the response. (Defined by AjaxExtensions.)
Public Extension Method BeginForm(String, String, RouteValueDictionary, AjaxOptions) Overloaded. Writes an opening <form> tag to the response. (Defined by AjaxExtensions.)
Public Extension Method BeginForm(String, String, Object, AjaxOptions, Object) Overloaded. Writes an opening <form> tag to the response. (Defined by AjaxExtensions.)
Public Extension Method BeginForm(String, String, RouteValueDictionary, AjaxOptions, IDictionary<String, Object>) Overloaded. Writes an opening <form> tag to the response. (Defined by AjaxExtensions.)
Public Extension Method BeginRouteForm(String, AjaxOptions) Overloaded. Writes an opening <form> tag to the response using the specified routing information. (Defined by AjaxExtensions.)
Public Extension Method BeginRouteForm(String, Object, AjaxOptions) Overloaded. Writes an opening <form> tag to the response using the specified routing information. (Defined by AjaxExtensions.)
Public Extension Method BeginRouteForm(String, RouteValueDictionary, AjaxOptions) Overloaded. Writes an opening <form> tag to the response using the specified routing information. (Defined by AjaxExtensions.)
Public Extension Method BeginRouteForm(String, Object, AjaxOptions, Object) Overloaded. Writes an opening <form> tag to the response using the specified routing information. (Defined by AjaxExtensions.)
Public Extension Method BeginRouteForm(String, RouteValueDictionary, AjaxOptions, IDictionary<String, Object>) Overloaded. Writes an opening <form> tag to the response using the specified routing information. (Defined by AjaxExtensions.)
Public Extension Method GlobalizationScript() Overloaded. Returns an HTML script element that contains a reference to a globalization script that defines the culture information. (Defined by AjaxExtensions.)
Public Extension Method GlobalizationScript(CultureInfo) Overloaded. Returns an HTML script element that contains a reference to a globalization script that defines the specified culture information. (Defined by AjaxExtensions.)
Public Extension Method RouteLink(String, Object, AjaxOptions) Overloaded. Returns an anchor element that contains the virtual path for the specified route values; when the link is clicked, a request is made to the virtual path asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method RouteLink(String, RouteValueDictionary, AjaxOptions) Overloaded. Returns an anchor element that contains the virtual path for the specified route values; when the link is clicked, a request is made to the virtual path asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method RouteLink(String, String, AjaxOptions) Overloaded. Returns an anchor element that contains the virtual path for the specified route values; when the link is clicked, a request is made to the virtual path asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method RouteLink(String, Object, AjaxOptions, Object) Overloaded. Returns an anchor element that contains the virtual path for the specified route values; when the link is clicked, a request is made to the virtual path asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method RouteLink(String, RouteValueDictionary, AjaxOptions, IDictionary<String, Object>) Overloaded. Returns an anchor element that contains the virtual path for the specified route values; when the link is clicked, a request is made to the virtual path asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method RouteLink(String, String, AjaxOptions, Object) Overloaded. Returns an anchor element that contains the virtual path for the specified route values; when the link is clicked, a request is made to the virtual path asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method RouteLink(String, String, AjaxOptions, IDictionary<String, Object>) Overloaded. Returns an anchor element that contains the virtual path for the specified route values; when the link is clicked, a request is made to the virtual path asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method RouteLink(String, String, Object, AjaxOptions) Overloaded. Returns an anchor element that contains the virtual path for the specified route values; when the link is clicked, a request is made to the virtual path asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method RouteLink(String, String, RouteValueDictionary, AjaxOptions) Overloaded. Returns an anchor element that contains the virtual path for the specified route values; when the link is clicked, a request is made to the virtual path asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method RouteLink(String, String, Object, AjaxOptions, Object) Overloaded. Returns an anchor element that contains the virtual path for the specified route values; when the link is clicked, a request is made to the virtual path asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method RouteLink(String, String, RouteValueDictionary, AjaxOptions, IDictionary<String, Object>) Overloaded. Returns an anchor element that contains the virtual path for the specified route values; when the link is clicked, a request is made to the virtual path asynchronously by using JavaScript. (Defined by AjaxExtensions.)
Public Extension Method RouteLink(String, String, String, String, String, RouteValueDictionary, AjaxOptions, IDictionary<String, Object>) Overloaded. Returns an anchor element that contains the virtual path for the specified route values; when the link is clicked, a request is made to the virtual path asynchronously by using JavaScript. (Defined by AjaxExtensions.)

Top

Remarks

The AjaxHelper class includes methods that provide client-side functionality in ASP.NET AJAX in MVC applications, such as creating asynchronous forms and rendering links. The AjaxHelper class supports asynchronous partial-page updates. Extensions to the AjaxHelper class are in the System.Web.Mvc.Ajax namespace.

Helper methods and extensions are called using the Ajax property of the view, which is an instance of the AjaxHelper class. For example, to generate a link to a controller action method, you can call the ActionLink method in your view using the following syntax:

<%= Ajax.ActionLink("ActionName") %>

Examples

The following example shows a view that calls the ActionLink method, the BeginRouteForm method, the BeginForm method, and the BeginRouteForm method. The ActionLink method links to an action method that updates a status message that contains a time stamp. The RouteLink method links to the same action method and updates a second status message. The BeginForm method creates a form that contains a text box where the user can enter a message that is displayed with the time the message was entered. The BeginRouteForm method creates a second form that also contains a text box for entering a message.

<h2><%= Html.Encode(ViewData("Message")) %></h2>
<p>
    Page Rendered: <%= DateTime.Now.ToLongTimeString() %>
</p>
<span id="status">No Status</span>
<p>
<%=Ajax.ActionLink("Update Status", "GetStatus", _
   New AjaxOptions With {.UpdateTargetId = "status"})%>
</p>
<span id="routeStatus">No Status</span>
<p>
<%=Ajax.RouteLink("Update Status with Route", _
   New RouteValueDictionary(New With {.controller = "Home", .action = "GetStatus"}), _
   New AjaxOptions With {.UpdateTargetId = "routeStatus"})%>
</p>
<p>
<%  Using (Ajax.BeginForm("UpdateForm", _
   New AjaxOptions With {.UpdateTargetId = "textEntered1"}))%>
   <%= Html.TextBox("textBox1","Enter text")%>  
   <input type="submit" value="Submit"/>
   <br />
   <span id="textEntered1">Nothing Entered</span>
<% End Using%>
</p>
<p>
<%  Using (Ajax.BeginRouteForm(Nothing, _
   New RouteValueDictionary(New With {.controller = "Home", .action = "UpdateRouteForm"}), _
   New AjaxOptions With {.UpdateTargetId = "textEntered2"}))%>
   <%=Html.TextBox("textBox2", "Enter text")%>  
   <input type="submit" value="Submit with Route"/>
   <br />
   <span id="textEntered2">Nothing Entered</span>
<% End Using%>
</p>
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<p>
Page Rendered: <%= DateTime.Now.ToLongTimeString() %>
</p>
<span id="status">No Status</span>
<p>
<%= Ajax.ActionLink("Update Status", "GetStatus", 
    new AjaxOptions{ UpdateTargetId = "status" }) %>
</p>
<span id="routeStatus">No Route Status</span>
<p>
<%= Ajax.RouteLink("Update Status with Route", 
    new RouteValueDictionary{{"controller", "Home"}, {"action", "GetStatus"}},
    new AjaxOptions { UpdateTargetId = "routeStatus" })%>
</p>
<p>
<% using(Ajax.BeginForm("UpdateForm", 
   new AjaxOptions{UpdateTargetId="textEntered1"})) { %>
     <%= Html.TextBox("textBox1","Enter text")%>  
     <input type="submit" value="Submit"/><br />
     <span id="textEntered1">Nothing Entered</span>
<% } %>
</p>
<p>
<% using(Ajax.BeginRouteForm( null,
   new RouteValueDictionary {{"controller", "Home"}, {"action", "UpdateRouteForm"}}, 
   new AjaxOptions{UpdateTargetId="textEntered2"})) { %>
     <%= Html.TextBox("textBox2","Enter text")%>  
     <input type="submit" value="Submit with Route"/><br />
     <span id="textEntered2">Nothing Entered</span>
<% } %>
</p>

To support AJAX in the view, the following script references must be added to the master view.

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>

The following example shows the HomeController class that supports the view that is listed in the previous example. The GetStatus method is called by both the ActionLink and RouteLink methods and returns a status string. The UpdateForm method is called when the form created by BeginForm is submitted. This method returns a message to be displayed. The UpdateRouteForm method is called when the form that is created by BeginRouteForm is submitted. This method also returns a message to display.

<HandleError()> _
Public Class HomeController
    Inherits System.Web.Mvc.Controller

    Function Index() As ActionResult
        ViewData("Message") = "ASP.NET MVC and AJAX"

        Return View()
    End Function

    Function About() As ActionResult
        Return View()
    End Function

    Public Function GetStatus() As String
        Return "Status OK at " + DateTime.Now.ToLongTimeString()
    End Function

    Public Function UpdateForm(ByVal textBox1 As String) As String
        If textBox1 <> "Enter text" Then
            Return "You entered: """ + textBox1.ToString() + """ at " + DateTime.Now.ToLongTimeString()
        End If

        Return [String].Empty
    End Function

    Public Function UpdateRouteForm(ByVal textBox2 As String) As String
        If textBox2 <> "Enter text" Then
            Return "You entered: """ + textBox2.ToString() + """ at " + DateTime.Now.ToLongTimeString()
        End If

        Return [String].Empty
    End Function
End Class

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 Namespace