. Ajax based MVC scheme in net environment

Source: Internet
Author: User
Tags implement reference
Ajax

1, the problem background
Now, more and more people are trying to make web development without refreshing based on Ajax, but in the. NET environment, it is not very convenient to apply Ajax, which may be caused by some of the following reasons:
? Because Ajax is based on the nature of JavaScript, the developer must have a good understanding of JavaScript, at least, its JavaScript ability to achieve the callback return content to the page update, so the threshold for the development of a certain degree of increase
When development based on Ajax mechanism, the original postback mode, asp.net by the background logic code (MODEL), aspx page (View), Aspx.cs (Controller) of the MVC framework is actually invalid, When callback returns data, either the return content is parsed with JavaScript on the client side to implement the update, or the more complete HTML code must be constructed on the server side, and then the constructed HTML is set to a Page object directly by JavaScript. Obviously, this way, to achieve one of the simplest callback functions, a lot of code, and is relatively messy code, even in the upcoming asp.net2.0 the problem will not be effectively resolved.

2. The purpose of this article
The purpose of this paper is to benefit the characteristics of the existing asp.net itself and Ajax features, and to propose an MVC scheme for Web development based on Ajax in the ASP.NET environment to achieve the following main purposes:
? A clear MVC framework for Ajax in asp.net environments
? Reduce the programmer's reliance on excessive JavaScript coding to reduce the programming threshold
? Flexible support for common development methods in AJAX mode

3, problem analysis
How to achieve the above several main purposes?
1 to the XMLHttpRequest for better encapsulation, so that the way to invoke simpler;
2) As far as possible in the server to update the structure of the data, but also to avoid each return data are manually constructed, so, think of the full use of UserControl, by UserControl as "View", corresponding by the Ascx.cs file as " Controller ", this composition of MVC is also relatively clear;

4, problem resolution
based on the above ideas, I implemented the following Group class library to simplify the process:
Source code and example download
Codes:
1 first on the client side, Ajaxhelper.js encapsulates the XMLHttpRequest and provides an existing serialization as Param1=v1?m2=v2& .... The parameters of the form used for post;
Updater (ajaxtemplate, output, params, oncomplete) function to implement a callback call
Ajaxtemplate (required): Specifies the UserControl path
output (optional) to perform the required function: fill in the reference or ID value of the specified label for the returned data
params (optional): Form param1=v1?m2=v2& ... The Post parameter
OnComplete (optional): A callback function that can be used for special processing of the returned data, function format functions (str), and STR for the returned data
Serializeform (form) to serialize the
Form: Can be on the server side of the specified reference or ID value
2), The ajax.aspx file encapsulates the invocation of the UserControl specified by the client ajaxtemplate, and the rest of the specific logic functions are implemented within a particular UserControl and its Ascx.cs;
3) When executing a callback, the programmer simply references the ajaxhelper.js in the page and passes through Javascript:updater (ajaxtemplate, output, params, oncomplete) at the specified location. is invoked, and if a form is required to be committed, the form can be serialized and passed to the params, and of course params can be constructed manually, Javascript:serializeform. and specifies that the returned data be processed by setting the page to which the output is applied, or through OnComplete customization.
4) Due to the full use of UserControl means that it is possible to take full advantage of the original Web server-side control and data binding mechanism of asp.net, which in fact has largely simplified the construction of the return data, in Ascx.cs, through request.form[ ParamName] can access the incoming params from the client side and access the logical code to obtain the source data.



Related Article

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.