MVC uses jquery to implement Ajax

Source: Internet
Author: User

When I introduced the Ajax Implementation Method in MVC in the previous article, I mentioned that jquery is another method to implement Ajax besides Ajax HTML helper.

Ajax request via get Method

View

 <  Script  Type  = "Text/JavaScript"> Function Gettime () {$. Get ( "Home/gettime" , Function (Response) {$ ( "# Mypnl" Response .html (response );}); Return false ;} </  Script  > < Div  ID  = "Mypnl"  Style  ="  Width  : 300px;  Height  : 30px;  Border  : 1px dotted silver; "> </  Div  > <  A  Href  = "#"  Onclick  = "Return gettime ();"> Click me </  A  > 

Controller

 
PublicActionresultGettime (){ReturnContent (Datetime. Now. tostring ());}
Use the POST method to implement form Ajax submission

View

 @ Model Mvcajax. models. Usermodel  @{ Viewbag. Title = "Ajaxform" ; }  <  Script Type  = "Text/JavaScript"> $ (Document). Ready ( Function () {$ ( "Form [Action $ = 'saveuser']" ). Submit ( Function () {$. Post ($ ( This ). ATTR ( "Action" ), $ ( This ). Serialize (), Function (Response) {$ ( "# Mypnl" Response .html (response );}); Return false ;});});</  Script  > <  Div  ID  = "Mypnl"  Style  ="  Width  : 300px;  Height  : 30px;  Border  : 1px dotted silver; "> </  Div  >  @  Using (Html. beginform ( "Saveuser" , "Home" )){ <  Table  > <  Tr  > <  TD  >  @ Html. labelfor (M => M. username) </  TD  > <  TD  > @ Html. textboxfor (M => M. username) </  TD  > </  Tr  > <  Tr  > <  TD  >  @ Html. labelfor (M => M. Email) </  TD  > <  TD >  @ Html. textboxfor (M => M. Email) </  TD  > </  Tr  > <  Tr  > <  TD  >  @ Html. labelfor (M => M. DESC) </  TD  > < TD  >  @ Html. textboxfor (M => M. DESC) </  TD  > </  Tr  > <  Tr  > <  TD  Colspan  = "2"> <  Input  Type  = "Submit" Value  = "Submit"/> </  TD  > </  Tr  > </  Table  > }

Model

 Using System. componentmodel. dataannotations; Namespace Mvcajax. Models { Public class  Usermodel {[ Display (Name = "Username" )] Public String Username { Get ; Set ;}[ Display (Name = "Email" )] Public String Email { Get ; Set ;}[ Display (Name = "Description" )] Public String Desc { Get ;Set ;}}}

Controller

 
[Httppost]PublicActionresultSaveuser (UsermodelUsermodel ){// Save User code here //......ReturnContent ("Save complete! ");}

AboveCodeImplements the jquery post data submission method.

By viewing the Ajax implementation methods of the above two jquery methods, we can see that the controller is the same as the previous Ajax HTML helper.

The main implementation scheme for data submission using jquery is to send a request to the Controller of MVC through jquery's get or POST method, process the obtained response, and update it to the page.

Note:

Whether you submit a request using a hyperlink or form, the JavaScript method always returns a value of false, which is used to prevent the jump of the hyperlink or the actual submission of the form.

There will be a question in this place:

If JavaScript scripts are disabled for this page, the page is redirected or the form is submitted, and the returned actionresult processing will be faulty.

In this case, the controller needs to determine whether the request is an Ajax request and return different actionresults based on different situations:

 
[Httppost]PublicActionresultSaveuser (UsermodelUsermodel ){// Save User code here //......If(Request. isajaxrequest ())ReturnContent ("Save complete! ");Else returnView();}
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.