ASP.NET MVC視圖頁使用jQuery傳遞異步數(shù)據(jù)的幾種方式詳解
在ASP.NET MVC的視圖頁向控制器傳遞異步數(shù)據(jù),可能是數(shù)組,JavaScript對象,json,表單數(shù)據(jù),等等。
關(guān)于數(shù)據(jù),JavaScript對象有時(shí)候和json長得一模一樣,有么有?
var person = {Name: 'darren', Age: 21};
以上是一個(gè)JavaScript對象。不過也可以這樣表示:
var person = {"Name":"darren","Age":21};
以上JavaScript對象的另外一種表達(dá)方式,恰恰也符合json的表達(dá)方式。不過,JavaScript對象的寫法推薦使用第一種方式。
關(guān)于異步ajax發(fā)送;data屬性表示傳遞的數(shù)據(jù);contentType樹形的默認(rèn)值是application/x-www-form-urlencoded,表示客戶端請求類型;dataType表示從服務(wù)端返回的類型,可以是text, xml, json, script, html, jsonp。
而在服務(wù)端,通過Request.Form屬性可以獲得從客戶端傳遞來的異步數(shù)據(jù)。
傳遞JavaScript對象
在Home/Index.cshtml視圖中,使用jQuery發(fā)出一個(gè)異步請求,把返回的html內(nèi)容加載到當(dāng)前視圖中。
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Index</h2> <div id="result"></div> @section scripts { <script type="text/javascript"> $(function() { var person = {Name: "Darren", Age: 21}; $.ajax({ type: "GET", url: "@Url.Action("GetInfo","Home")", data: person, datatype: "html", success:function(data) { $("#result").html(data); } }); }); </script> }
HomeController中從Request.Form中獲取數(shù)據(jù),并返回強(qiáng)類型部分視圖。
public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult GetInfo() { //從表單中獲取的數(shù)據(jù) var person = new Person(); person.Name = Request["Name"]; person.Age = int.Parse(Request["Age"]); return PartialView("_DisplayJavaScriptObject", person); } }
Home/_DisplayJavaScriptObject.cshtml強(qiáng)類型視圖展示數(shù)據(jù)。
@model MvcApplication1.Models.Person <div> <h3>從表單中讀出的數(shù)據(jù)</h3> <p><span>Name:</span><span>@Model.Name</span></p> <p><span>Age:</span><span>@Model.Age</span></p> </div>
傳遞數(shù)組
傳遞數(shù)組的時(shí)候有幾點(diǎn)需注意:
1、需要把數(shù)組toString()后作為json數(shù)據(jù)傳遞,toString()后數(shù)組變成以逗號隔開的字符串
2、是以Query String的形式傳遞給服務(wù)端的
3、服務(wù)端為了獲取集合數(shù)據(jù),需要split數(shù)組字符串
在Home/Index.cshtml視圖中,使用jQuery發(fā)送異步GET請求時(shí),數(shù)組ids需要toString()轉(zhuǎn)換成"1,2,3"形式的字符串。
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Index</h2> <div id="result"></div> @section scripts { <script type="text/javascript"> $(function() { var ids = [1, 2, 3]; $.ajax({ type: "GET", url: "@Url.Action("GetInfo","Home")", data: { myArr: ids.toString() }, datatype: "html", success:function(data) { $("#result").html(data); } }); }); </script> }
在HomeController中,通過Request.QueryString來獲取數(shù)組字符串,最后再split轉(zhuǎn)換成集合。
public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult GetInfo() { string temp = Request.QueryString["myArr"]; List<int> result = new List<int>(); string[] tempArr = temp.Split(","); foreach (var item in tempArr) { result.Add(int.Parse(item)); } ViewData["t"] = result; return PartialView("_DisplayJavaScriptObject"); } }
Home/_DisplayJavaScriptObject.cshtml從ViewData中取出數(shù)據(jù)遍歷集合展示數(shù)據(jù)。
@foreach (var item in ViewData["t"] as IEnumerable<int>) { <span>@item.ToString()</span> }
傳遞表單數(shù)據(jù)
傳遞表單數(shù)據(jù)的時(shí)候有幾點(diǎn)需注意:
1、通過$('#myForm').serialize()把表單數(shù)據(jù)封裝起來
2、控制器Action方法需要打上[HttpPost]
3、控制器Action方法,可以通過強(qiáng)類型參數(shù)來接收,也可通過Request["Name"]的方式獲取數(shù)據(jù)
在Home/Index.cshtml視圖中,使用jQuery發(fā)送異步POST請求時(shí),使用$('#myForm').serialize()把表單數(shù)據(jù)封裝起來。
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Index</h2> <div> <form id="myForm"> <div> @Html.Label("Name","姓名") @Html.TextBox("Name","Darren") </div> <div> @Html.Label("Age","年齡") @Html.TextBox("Age","21") </div> </form> <div> <button id="btn">提交</button> </div> </div> <div id="result"></div> @section scripts { <script type="text/javascript"> $(function() { $("#btn").on("click", function() { $.ajax({ type: "POST", url: "@Url.Action("GetInfo","Home")", data: $("#myForm").serialize(), dataType: "html", success: function(data) { $("#result").html(data); } }); }); }); </script> }
在HomeController中,需要在GetInfo方法上打上[HttpPost],用強(qiáng)類型參數(shù)來接收數(shù)據(jù)。
public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpPost] public ActionResult GetInfo(Person person) { return PartialView("_DisplayJavaScriptObject", person); } }
Home/_DisplayJavaScriptObject.cshtml強(qiáng)類型視圖展示數(shù)據(jù)。
@model MvcApplication1.Models.Person <div> <h3>從表單中讀出的數(shù)據(jù)</h3> <p><span>Name:</span><span>@Model.Name</span></p> <p><span>Age:</span><span>@Model.Age</span></p> </div>
傳遞json數(shù)據(jù)
傳遞json數(shù)據(jù)需注意的是:
1、json格式要寫對:{ "Name":"Darren","Age":21}
2、控制器Action方法中通過Request["Name"]的方式獲取數(shù)據(jù)
在Home/Index.cshtml視圖中,使用jQuery發(fā)送異步Get請求。
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Index</h2> <div id="result"></div> @section scripts { <script type="text/javascript"> $(function() { $.ajax({ type: "GET", url: "@Url.Action("GetInfo","Home")", data: { "Name":"Darren","Age":21}, datatype: "html", success:function(data) { $("#result").html(data); } }); }); </script> }
在HomeController中通過Request["Name"]的方式獲取數(shù)據(jù)。
public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult GetInfo() { //從表單中獲取的數(shù)據(jù) var person = new Person(); person.Name = Request["Name"]; person.Age = int.Parse(Request["Age"]); return PartialView("_DisplayJavaScriptObject", person); } }
Home/_DisplayJavaScriptObject.cshtml強(qiáng)類型視圖展示數(shù)據(jù)。
@model MvcApplication1.Models.Person <div> <h3>從表單中讀出的數(shù)據(jù)</h3> <p><span>Name:</span><span>@Model.Name</span></p> <p><span>Age:</span><span>@Model.Age</span></p> </div>
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章:
1. ASP.NET MVC通過勾選checkbox更改select的內(nèi)容2. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁3. ASP.NET MVC使用JSAjaxFileUploader插件實(shí)現(xiàn)單文件上傳4. ASP.NET MVC增加一條記錄同時(shí)添加N條集合屬性所對應(yīng)的個(gè)體5. ASP.NET MVC實(shí)現(xiàn)單個(gè)圖片上傳、限制圖片格式與大小并在服務(wù)端裁剪圖片6. ASP.NET MVC解決上傳圖片臟數(shù)據(jù)的方法7. ASP.NET MVC限制同一個(gè)IP地址單位時(shí)間間隔內(nèi)的請求次數(shù)8. ASP.NET MVC實(shí)現(xiàn)橫向展示購物車9. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值10. ASP.NET MVC實(shí)現(xiàn)區(qū)域或城市選擇
