Article transferred from http://www.huiyoumi.wang/upload/forum.php?mod=viewthread&tid=75&extra=
ASP. NET MVC discards the highly encapsulated controls of ASP. WebForm, which gives us more familiarity with the underlying HTML. You can control the structure, style, and behavior of HTML more freely and flexibly. And this is really a lot better for Ajax than WebForm. I made a discussion about ASP. NET MVC more convenient to use Ajax, to share, welcome to shoot bricks. The following three ways to demonstrate the use of Ajax, both with a simple demo show you want to study more, ask questions and share experience.
First, directly write the JS code to achieve Ajax:
Because the code in the ASP is better controlled, we can make AJAX requests directly in HTML via JS, and return XML or JSON in the Controller's action to implement the Ajax effect, which is the most primitive method. Browser and many issues need to be considered, not recommended.
The reference code is as follows: JS code:
- var xhr;
- function Getxhr ()
- {
- try {
- Xhr=new ActiveXObject ("msxml2.xmlhttp");
- } catch (e) {
- try {
- Xhr=new ActiveXObject ("Microsoft.XMLHTTP");
- } catch (e) {
- Xhr=false;
- }
- }
- if (!xhr&&typeof xmlhttprequest!= ' undefined ')
- {
- Xhr=new XMLHttpRequest ();
- }
- return XHR;
- }
- function Openxhr (method,url,callback)
- {
- GETXHR ();
- Xhr.open (Method,url);
- Xhr.onreadystatechange=function ()
- {
- if (xhr.readystate!=4) return;
- Callback (XHR);
- }
- Xhr.send (NULL);
- }
- function LoadXML (method,url,callback)
- {
- GETXHR ();
- Xhr.open (Method,url);
- Xhr.setrequestheader ("Content-type", "Text/xml");
- Xhr.setrequestheader ("Content-type", "GBK");
- Xhr.onreadystatechange=function ()
- {
- if (xhr.readystate!=4) return;
- Callback (XHR);
- }
- Xhr.send (NULL);
- }
Copy Code
Background code:
- Public ActionResult getnews (int CategoryID)
- {
- Newscollectionmodel newscollection = new Newscollectionmodel ();
- ............
- Jsonresult Myjsonresult = new Jsonresult ();
- Myjsonresult = newscollection;
- return myjsonresult;
- }
Copy Code
The second type: Ajax calls with jquery:
In VS 2010, the IDE fully supports the Smart Display of jquery, the use of JQ in development to achieve the JS effect is very good, and can save a lot of energy and time. So it's also a good idea to use jquery for development in Ajax.
The approximate implementation code is as follows:
- <% using (Html.BeginForm ("AddComment", "Comment", formmethod.post,new {@class = "Hijax"})) {%>
- <%= Html.textarea ("Comment", New{rows=5, cols=50})%>
- <button type= "Submit" >add comment</button>
- <span id= "indicator" style= "Display:none" ></span>
- <%}%>
- To reference jquery in view:
- <script src= "Http://www.cnblogs.com/Scripts/jquery-1.4.1.min.js" type= "Text/javascript" ></script>
- Add the following script:
- <script type= "Text/javascript" >
- Execute when the DOM has been loaded
- $ (document). Ready (function () {
- Wire up to the form submit event
- $ ("Form.hijax"). Submit (Function (event) {
- Event.preventdefault (); Prevent the actual form post
- Hijack (this, update_sessions, "html");
- });
- });
- function hijack (form, callback, format) {
- $ ("#indicator"). Show ();
- $.ajax ({
- Url:form.action,
- Type:form.method,
- Datatype:format,
- Data: $ (form). Serialize (),
- Completed: $ ("#indicator"). Hide (),
- Success:callback
- });
- }
- function Update_sessions (Result) {
- Clear the form
- $ ("Form.hijax") [0].reset ();
- $ ("#comments"). Append (result);
- }
- </script>
Copy Code
The third method: Use Microsoft's own AJAX helper framework to implement.
- <% using (Ajax.beginform ("AddComment", New ajaxoptions
- {
- HttpMethod = "POST",
- Updatetargetid = "Comments",
- Insertionmode = Insertionmode.insertafter
- })) {%>
- <%= Html.textarea ("Comment", New{rows=5, cols=50})%>
- <button type= "Submit" >add comment</button>
- <%}%>
Copy Code
The second and third method in the blog park has a blog, I have a direct reference, the specific address is as follows:http://www.cnblogs.com/zhuqil/archive/2010/07/18/1780285.html
It is primarily a matter of my own opinion and attention to the Ajax helper record.
First, Ajax Helper is a Microsoft-provided AJAX framework, in order to use AJAX helper must use the Microsoft provided two JS framework:
<script src= "Http://www.cnblogs.com/Scripts/MicrosoftAjax.js" type= "Text/javascript" ></script> < Script src= "Http://www.cnblogs.com/Scripts/MicrosoftMvcAjax.js" type= "Text/javascript" ></script>
Second, Ajax Helper has several uses
Ajax.actionlink (): It renders a hyperlink tag, similar to Html.ActionLink (). When it is clicked, it gets the new content and inserts it into the HTML page.
ajax.beginform (): It renders as an HTML form form, similar to Html.BeginForm (). When it commits, it gets the new content and inserts it into the HTML page.
Ajax.routelink (): Ajax.routelink () is similar to Ajax.actionlink (). However, it can generate a URL based on any of the routing parameters, without having to include the invoked action. The most used scene is a custom icontroller with no action in it.
ajax.beginrouteform (): same ajax.beginrouteform () similar to Ajax.beginform (). This Ajax equates to Html.routelink ().
and the parameters in each method will be different, the specific usage see:http://msdn.microsoft.com/zh-cn/library/system.web.mvc.ajaxhelper_methods (v=vs.98). ASPX
One of the important parameters is: ajaxoption, there are several properties, mainly to specify the behavior of Ajax.
The use of Ajax in ASP. NET MVC [share]