ASP.NET MVC中如何以ajax的方式在View和Action中傳遞數據

前言:寫這篇隨筆的時候,在url上漏寫了斜線,找了很久錯誤,整我的都很很差。#我是豬系列html

背景:以前介紹過一篇如何構建 MVC&AJax&JSon示例,這一篇單獨講解如何在View和Action間傳遞並處理數據。ajax

1,前臺HTML代碼:json

1     <div>
2         <button type="button" id="btn">從視圖向控制器中傳遞數據</button>
3         <p id="info"></p>
4     </div>

2,前臺JS代碼:dom

複製代碼
 1         $("#btn").click(function() {
 2             $.ajax({
 3                 async:true,
 4                 type: "POST",
 5                 url: "/AjaxTest/AjaxBackData",
 6                 cache: false,
 7                 data: {
 8                     Name: "SharpL", City: "北京", Age: 18
 9                 },
10                 success: function (result) {
11                     $("#info").text(result);
12                 }
13             });
14         });
複製代碼

JS代碼講解:注意url: "/AjaxTest/AjaxBackData",AjaxTest前的‘/'千萬不能漏掉,博主已經開始懷疑人生了。async

     data: {Name: "SharpL", City: "北京", Age: 18},數據就是這樣以匿名對象的方式傳遞過去的。ide

或者JS代碼這樣來寫:post

複製代碼
 1     $(function () {
 2         $("#btn").click(function () {
 3             var data = "";
 4             data += "&Name=" +encodeURI("SharpL");
 5             data += "&Age=" + encodeURI(18);
 6             data += "&City=" + encodeURI("北京");
 7             $.ajax({
 8                 async:true,
 9                 type: "POST",
10                 url: "/AjaxTest/AjaxBackData",
11                 cache: false,
12                 data: data,
13                 success: function (result) {
14                     $("#info").text(result);
15                 }
16             });
17         });
18     });
複製代碼

或者JS代碼這樣來寫:this

複製代碼
 1     $(function () {
 2         $("#btn1").click(function () {
 3             $.ajax({
 4                 type: "POST",
 5                 url: "/TestAjax/Test?City=北京&Name=SharpL&Age=18",
 6                 cache: false,
 7                 data: null,
 8                 success: function (result) {
 9                     if (result) {
10                         $("#pDisplay").text("返回信息爲 " + result.Message + "\n" + "隨機數爲" + result.RandomNum);
11                     }
12                 }
13                 });
14         });
15     })
複製代碼

三者的結果徹底相同。url

3,後臺代碼以下:spa

複製代碼
1         public ActionResult AjaxBackData(STU stu)
2         {
3             string name = stu.Name;
4             int age = stu.Age;
5             string city = stu.City;
6             string tmp=string.Format("{0}年齡{1}歲,來自{2}",name,age,city);
7             return Content(tmp);
8         }
複製代碼

注意Action的參數爲STU,直接獲取以ajax方式傳遞過來的數據。

或者後臺代碼以下:(項目中所使用的經典方式)

複製代碼
1         public ActionResult AjaxBackData()
2         {
3             var stu = new STU();
4             this.UpdateModel(stu);
5             string tmp=string.Format("{0}年齡{1}歲,來自{2}",stu.Name,stu.Age,stu.City);
6             return Content(tmp);
7         }
複製代碼

或者後臺代碼以下:(以顯示ContentResult的方式返回)前兩種方式返回Content,其返回值仍然是ContentResult。

複製代碼
1             var actionResult = default(ContentResult);
2             var stu =new Stu();
3             this.UpdateModel(stu);
4             actionResult=new ContentResult(){
5                 Content=string.Format("{0}{1}歲,來自{2}",stu.Name,stu.Age,stu.City)
6             };
7             return actionResult;
複製代碼

Content只可以返回Content,當須要返回多項數據時,返回Json(),代碼以下:

複製代碼
1         public ActionResult Test()
2         {
3             var stu = new Stu();
4             this.UpdateModel(stu);
5             var tmp= string.Format("{0}{1}歲,來自{2}", stu.Name, stu.Age, stu.City);
6             Random r=new Random();
7             int t=r.Next(1,10);
8             return Json(new { Message = tmp, RandomNum = t });
9         }
複製代碼

2.2同時修改對於返回json格式的數據的前臺Ajax接收的代碼,修改以下:

1                 success: function (result) {
2                     if (result) {
3                         $("#pDisplay").text("返回信息爲 " + result.Message + "\n" + "隨機數爲" + result.RandomNum);
4                     }
5                 }

相似的,能夠將Json修改成顯式返回JsonResult對象,代碼以下:

複製代碼
 1         public ActionResult Test()
 2         {
 3             var actionResult = default(JsonResult);
 4             var stu = new Stu();
 5             this.UpdateModel(stu);
 6             var tmp= string.Format("{0}{1}歲,來自{2}", stu.Name, stu.Age, stu.City);
 7             Random r=new Random();
 8             int t=r.Next(1,10);
 9             actionResult = new JsonResult()
10             {
11                 Data = new { Message = tmp, RandomNum = t }
12             };
13             return actionResult;
14         }
複製代碼

 



出處:http://www.cnblogs.com/SharpL/p/4681596.html

相關文章
相關標籤/搜索