MVC&WEBFOMR-controlled Learning: Ajax Asynchronous request

Source: Internet
Author: User

Written in front: because of the need for work, I have just contacted ASP. NET MVC, although WebForm's project has done several. But it's not very proficient. Put aside the pros and cons of ASP. NET WebForm and ASP. I think that although there are many differences between MVC and WebForm, it is only in order to achieve the same purpose and to take different means under the respective mechanism. Not to mention that they are all based on the ASP. So when it comes to solving the problems in the MVC development process, I can easily think of how the same problem is solved under WebForm. Take this as a comparison. Let me just call this approach a migration study. I have a "child's heart" to speculate that there must be a lot of predecessors have done it.

A few days ago someone asked a question: in WebForm, can an asynchronous request jump to a method in the background of the current page? Just like the action in MVC that jumps to the current controller. I gave a reference plan at the time: using WebMethod. Yes, it's about this topic today. Simply talk about Ajax asynchronous requests in WebForm and MVC.

Ajax asynchronous requests in Webfrom

Let's pull an egg first. We know that in order to achieve the separation of model display and logic, WebForm uses event-driven. This is the event operation that registers the corresponding operation beforehand. As a result, each time the server control's event is triggered, the page load will be a bit. The corresponding event logic is then executed. One consequence of this is to initialize the page's data every time. In order to balance, the ViewState is used to filter the repetitive initialization binding work by adding if (page.ispostback) judgment in the Page_Load event. This, of course, triggers a server callback that causes a global refresh of the page (although data requests that initialize data binding are omitted). So with all that crap, you naturally think of a way to stop this behavior. We know that the ASPX page actually last requested the page itself (that is, the action that the form form generates points to itself). In this, we seem to be able to perceive something. That is, how to implement the request itself without causing the interface to be completely refreshed, it is natural to think of a technique: callback. Having said so much, come to the section code review under WebForm using jquery to implement asynchronous requests.

aspx:<%@ Page language="C #"autoeventwireup="true"Codebehind="Index.aspx.cs"inherits="Weixin_api. Index"%><! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Server"> <script src="Scripts/jquery-1.4.1.min.js"></script> <meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/> <title></title> <script>$ (function () {$ ("#btnTest"). Click (function () {$.ajax ({type:"POST", ContentType:"Application/json", URL:"Index.aspx/sayhello", Data:NULL, DataType:"JSON", Success:function (msg) {alert (MSG.D);            }                });        });    }); </script>"Form1"runat="Server"> <div> <input type="Button"Id="btntest"Value="Test"/> </div> </form></body>aspx.cs:[System.Web.Services.WebMethod]  Public Static stringSayHello () {return "Hello";}

It is not difficult to see from the WebMethod attribute that this is actually implemented in the way that the WebService component is applied.

Ajax asynchronous requests in MVC

Because MVC uses route-based mapping, the action in the Controller can be requested directly through a URL. Here is an example of implementing jquery partial refresh in MVC

@model ienumerable<mvcappdemo.models.moviedb>@{Viewbag.title="List";}<p> <a href="#"Id="ADD"> Add </a> <div id="Create"></div></p><table> <tr> <th>@Html. Displaynamefor (Model=model. Title)</th> <th>@Html. Displaynamefor (Model=model. Director)</th> <th>@Html. Displaynamefor (Model=model. Date)</th> <th>Delete</th> </tr>@if (Model!=NULL&& model.count ()! =0)    {        foreach(varIteminchModel) {            <tr> <td>@Html. Displayfor (ModelItem=item. Title)</td> <td>@Html. Displayfor (ModelItem=item. Director)</td> <td>@Html. Displayfor (ModelItem=item. Date)</td> <td></td> </tr>        }    }</table><script src="~/scripts/jquery-1.8.2.min.js"></script><script type="Text/javascript">    $("#Add"). Click (function () {$.Get("/movies/create", Function (response) {$ ("#create"). HTML (response);        }); return false; });}

MVC&WEBFOMR-controlled Learning: Ajax Asynchronous request

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.