How to pass data in the view and action in an AJAX fashion in ASP.

Source: Internet
Author: User
Tags button type

Preface: Writing this essay, the URL of the omission of the slash, looking for a long time error, the whole people are very bad. #我是猪系列

Background: A previous article on how to build the Mvc&ajax&json sample, which explains how to pass and process data between view and action, is explained in a separate article.

1, foreground HTML code:

1     <div>2         <button type="button" id="btn" > Passing data from the view to the controller </button>3         <p id="info"></ P>4     </div>
View Code

2, front desk JS code:

1$("#btn"). Click (function () {2 $.ajax ({3                 Async:true,4Type"POST",5Url:"/ajaxtest/ajaxbackdata",6Cachefalse,7 data: {8Name:"SHARPL", City:"Beijing", Age: -9                 },Ten success:function (Result) { One$("#info"). Text (result); A                 } -             }); -});
View Code

JS Code Explanation: Note the URL: "/ajaxtest/ajaxbackdata", ajaxtest before the '/' must not miss, Bo Master has begun to suspect life.

     Data: {Name: "Sharpl", City: "Beijing", age:18}, this is the way to pass the past as an anonymous object.

Or JS code to write like this:

1 $ (function () {2$("#btn"). Click (function () {3             vardata ="";4Data + ="&name="+encodeuri ("SHARPL");5Data + ="&age="+ encodeURI ( -);6Data + ="&city="+ encodeURI ("Beijing");7 $.ajax ({8                 Async:true,9Type"POST",TenUrl:"/ajaxtest/ajaxbackdata", OneCachefalse, A Data:data, - success:function (Result) { -$("#info"). Text (result); the                 } -             }); -         }); -});
View Code

Or JS code to write like this:

1 $ (function () {2$("#btn1"). Click (function () {3 $.ajax ({4Type"POST",5Url:"/testajax/test? city= Beijing &name=sharpl&age=18",6Cachefalse,7DataNULL,8 success:function (Result) {9                     if(Result) {Ten$("#pDisplay"). Text ("return information for"+ result. Message +"\ n"+"the random number is"+result. Randomnum); One                     } A                 } -                 }); -         }); the})
View Code

The results of the three were identical.

3, the background code is as follows:

1          Publicactionresult Ajaxbackdata (STU STU)2         {3             stringName =Stu. Name;4             intAge =Stu. Age;5             stringCity =Stu. City;6             stringtmp=string. Format ("{0} age {1} years, from {2}", name,age,city);7             returnContent (TMP);8}
View Code

Note that the action parameter is Stu, which directly obtains the data that is passed in Ajax mode.

Or the background code is as follows: (classic method used in the project)

1          Publicactionresult ajaxbackdata ()2         {3             varStu =NewSTU ();4              This. Updatemodel (Stu);5             stringtmp=string. Format ("{0} age {1} years, from {2}", Stu. Name,stu. Age,stu. City);6             returnContent (TMP);7}
View Code

Or the background code is as follows: (return as shown Contentresult) The first two ways to return content, whose return value is still contentresult.

1             varActionResult =default(Contentresult);2             varStu =NewStu ();3              This. Updatemodel (Stu);4actionresult=NewContentresult () {5Content=string. Format ("{0}{1} years old, from {2}", Stu. Name,stu. Age,stu. City)6             };7             returnActionResult;
View Code

Content is only able to return content, and when multiple data needs to be returned, the JSON () is returned with the following code:

1          Publicactionresult Test ()2         {3             varStu =NewStu ();4              This. Updatemodel (Stu);5             vartmp=string. Format ("{0}{1} years old, from {2}", Stu. Name, Stu. Age, Stu. City);6Random r=NewRandom ();7             intT=r.next (1,Ten);8             returnJson (New{Message = tmp, Randomnum =T});9}
View Code

2.2 Modify the code for the foreground AJAX reception for data returned in JSON format , as follows:

1 success:function (Result) {2                     if(Result) {3$("#pDisplay"). Text ("return information for"+ result. Message +"\ n"+"the random number is"+result. Randomnum);4                     }5}
View Code

Similarly, you can modify the JSON to explicitly return an Jsonresult object with the following code:

1          Publicactionresult Test ()2         {3             varActionResult =default(Jsonresult);4             varStu =NewStu ();5              This. Updatemodel (Stu);6             vartmp=string. Format ("{0}{1} years old, from {2}", Stu. Name, Stu. Age, Stu. City);7Random r=NewRandom ();8             intT=r.next (1,Ten);9ActionResult =NewJsonresult ()Ten             { OneData =New{Message = tmp, Randomnum =T} A             }; -             returnActionResult; -}
View Code



Source: http://www.cnblogs.com/SharpL/p/4681596.html

How to pass data in the view and action in an AJAX fashion in ASP.

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.