ASP. NET MVC2 is so simple to use AJAX

Source: Internet
Author: User

Using AJAX in ASP. net mvc programming is very simple. With the help of the JQuery framework, you can also add other excellent features and make AJAX calls very simple. In this article, we will discuss the programming content in this aspect.

1. detect an AJAX request

A typical method for creating AJAX requests in Web forms is to use an update panel. When the update panel is sent back, ScriptManager provides a method (through its isInAsyncPostBack attribute) to detect AJAX sending back on the server. ASP. net mvc also provides a similar mechanism. Because AJAX can call behavior methods, we need a method to detect whether a request is a standard GET/POST operation or an AJAX request.

We can use a useful extension method to implement the above detection. You only need to call the Request. IsAjaxRequest () method. We know that a Web request is a reference to HttpRequestBase (a wrapper for the. NET Framework HttpRequest object) and is also a controller attribute. Therefore, we can use the IsAjaxRequest () method to check the request information to ensure that an AJAX request is being sent to the server. In this way, we can correctly detect whether the current request is an ASP. NET Request or a jQuery request.

To achieve this, you only need to add the following code in your behavior method.

Listing 1-detecting an AJAX request

If (Request. IsAjaxRequest ())

// AJAX request

Else

// Standard request

Why is it so important to detect an AJAX request? According to the task implemented by the behavior method, the AJAX request processing method is different from the standard request. We will soon see such an example.

Ii. Use Ajax forms

Microsoft provides a set of AJAX extensions, one of which is an Ajax form. Through this Ajax form, you can send asynchronous requests to the server and provide relevant response content. All client AJAX calling mechanisms are included in a MicrosoftMVCAjax script. Listing 2 provides a simple Ajax form example.

Listing 2: Using AJAX. BeginForm Extension

<% Using (Ajax. BeginForm ("AjaxCustom", new AjaxOptions {UpdateTargetId = "customresponse"}) {%>

Name: <% = Html. TextBox ("CustomName") %>

Value: <% = Html. TextBox ("CustomValue") %>

<% }%>

The preceding AJAX form processes the details of sending back, and uses JavaScript to execute it asynchronously, instead of executing a standard HTTP sending back to the server. In Listing 2, the form sends the data back to the AjaxCustom behavior method, passes the form value, and finally obtains the returned response content and uses the result in some way.

Next, you can use the very ready-made Request. IsAjaxRequest method in the behavior method:

Listing 3: handling possible Ajax return and providing responses

[HttpPost]

Public ActionResult AjaxCustom (FormCollection post)

{

If (Request. IsAjaxRequest ())

Return Content ("Success ");

Else

Return RedirectToAction ("Index ");

}

For an AJAX request, a special string is passed back to the client and processed accordingly. This row is controlled by the AjaxOptions set. In Listing 2, the string "Success" is injected into an HTML element with the id "customresponse. If for some reason the request is not from an AJAX call, the behavior method can still work-redirect to another view. We will discuss these issues in future articles.

The above is just a brief introduction to the use of MS Ajax, because I believe the best AJAX solution is to use JQuery.

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.