Use ajax to submit data in MVC. Use the jquery Ajax method to pass values to the action.

Source: Internet
Author: User

Upload values to action using jquery Ajax

 <  Script  Type  = "Text/JavaScript"  >  $ (Document). Ready (  Function  () {$ (  "  # BTN  "  ). Click ( Function  () {$. Ajax ({type:  '  Post  '  , URL:  "  /Home/myajax  "  , Data: {val1: $ ( "  # Txt1  "  ). Val (), val2: $ (  "  # Txt2  "  ). Val (), val3: $ (  "  # Txt3  " ). Val (), val4: $ (  "  # Txt4  "  ). Val (),}, datatype:  "  JSON  "  });});});  </  Script >  <  Input  ID  = "BTN"  Type  = "Button"  Value  = "Click"   />  <  Input  ID  = "Txt1"  Type  = "Text"  Value = ""   />  <  Input  ID  = "Txt2"  Type  = "Text"  Value  = ""   />  <  Input  ID  = "Txt3"  Type  = "Text" Value  = ""   />  <  Input  ID  = "Txt4"  Type  = "Text"  Value  = ""   /> 
 
Data is JSON data. The action to be passed is/Home/myajax. The method to receive the message in the Action method is as follows:
 Public Actionresult myajax ( String Val1 ){  String Val2 = request [ "  Val2  "  ]. Tostring ();  String Val3 = request. Form [ "  Val3  "  ]. Tostring ();  String Val4 = request. Params [ "  Val4  " ]. Tostring ();  Return Content ( "  Viewusercontrol1  "  );} 
 
Or the received parameter is formcollection, which has the same effect.
 Public  Actionresult myajax (formcollection f ){  String Val2 = f [ "  Val2  "  ]. Tostring ();  String Val3 = f [ "  Val3  "  ]. Tostring ();  String Val4 = f [ "  Val4  "  ]. Tostring ();  Return Content ( "  Viewusercontrol1  "  );} 
The strength of mvc3 is that it is based on the mechanism of variable parameter name matching, that is, it tries its best to find values with the same variable name.

 
For the above example, we can even construct a class, as shown below:
 Public   Class  Aclass {  Public   String Val1 { Set ; Get  ;}  Public   String Val2 { Set ; Get ;}  Public   String Val3 { Set ; Get  ;}  Public   String Val4 { Set ; Get  ;}} 
 
You can set the parameter type to Aclass.
 PublicActionresult myajax (Aclass f ){ReturnContent (F. val1 + F. val2 + F. val3 +F. val4 );}
 
Note: The attribute name of the Aclass class is the JSON key name. It can be matched as long as it complies with the same name.

Post: http://cnn237111.blog.51cto.com/2359144/838081

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.