ASP.NET MVC中如何以ajax的方式在View和Action中传递数据
前言:寫這篇隨筆的時候,在url上漏寫了斜線,找了好久錯誤,整個人都很不好。#我是豬系列
背景:之前介紹過一篇如何構建ASP.NET MVC4&JQuery&AJax&JSon示例,這一篇單獨講解如何在View和Action間傳遞并處理數據。
1,前臺HTML代碼:
1 <div> 2 <button type="button" id="btn">從視圖向控制器中傳遞數據</button> 3 <p id="info"></p> 4 </div> View Code2,前臺JS代碼:
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 }); View CodeJS代碼講解:注意url: "/AjaxTest/AjaxBackData",AjaxTest前的‘/'千萬不能漏掉,博主已經開始懷疑人生了。
data: {Name: "SharpL", City: "北京", Age: 18},數據就是這樣以匿名對象的方式傳遞過去的。
或者JS代碼這樣來寫:
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 }); View Code或者JS代碼這樣來寫:
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 }) View Code三者的結果完全相同。
3,后臺代碼如下:
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 } View Code注意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 } View Code或者后臺代碼如下:(以顯示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; View CodeContent只能夠返回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 } View Code2.2同時前臺Ajax接收的代碼修改如下:
1 success: function (result) { 2 if (result) { 3 $("#pDisplay").text("返回信息為 " + result.Message + "\n" + "隨機數為" + result.RandomNum); 4 } 5 } View Code類似的,可以將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 } View Code?
?
?
轉載于:https://www.cnblogs.com/SharpL/p/4681596.html
總結
以上是生活随笔為你收集整理的ASP.NET MVC中如何以ajax的方式在View和Action中传递数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 论软件的模块化与架构
- 下一篇: EasyUI DataGrid 合并单元