当 Create.cshtml 视图加载完成并浮现 Html 页面时,jquery.unobtrusive-ajax.js 库会寻找所有 data-ajax == true的元素,然后按照其它以 data-ajax 开头的属性值,jQuery 库中的函数将知道如何去执行 Ajax 请求。
2、基于JQuery的Ajax(1)使用JQuery的Ajax请求返回值为 Json格式的Controller方法
原理就是用JQuery的Ajax方法请求Action方法,返回值设为JSON,然后对JSON数据进行处理,例如用js函数进行处理
举个栗子:
<script type="text/javascript" language="javascript"> $(function(){ GetRoomInfoList();}); function GetRoomInfoList() { showDivLoading();//异步加载数据时的浮层 $.ajax({ type: "Post", url: "@Url.Content("~/Room/GetRoomInfoShipId")",//异步请求的URL,就是Room控制器方法GetRoomInfoShipId(long shipId) dataType: "json",//要求返回数据为JSON格式 data:{shipId:$("#ShipIdForSearch").val()},//异步请求的参数 success: function(data){ $("#RoomInfoListTable").empty(); //清空里面的所有内容 $.each(data, function(i, item){ //用js拼字符串处理数据,这里是显示所有房型列表信息 var str="<tr>"; str+=" <td>"; str+=" <span style=\" width:150px;display:block;white-space:nowrap; overflow:hidden; text-overflow:ellipsis;\">"; str+=item.BaseRoomId; str+=" </span>" str+=" </td>"; str+=" <td>"; str+=" <span style=\" width:150px;display:block;white-space:nowrap; overflow:hidden; text-overflow:ellipsis;\">"; str+=item.ShipId; str+=" </span>" str+=" </td>"; str+=" <td>"; str+=" <span style=\" width:150px;display:block;white-space:nowrap; overflow:hidden; text-overflow:ellipsis;\">"; str+=item.RoomType; str+=" </span>" str+=" </td>"; str+=" <td>"; str+=" <span style=\" width:150px;display:block;white-space:nowrap; overflow:hidden; text-overflow:ellipsis;\">"; str+=item.RoomName; str+=" </span>" str+=" </td>"; str+="</tr>"; $("#RoomInfoListTable").append(str);
}); } }); }
(2)使用JQuery的Ajax 请求返回值为PartialView格式的Controller方法
假设有这样的一个Model:
namespace MvcApplication1.Models { public class Team { public string Preletter { get; set; } public string Name { get; set; } } }通过JQuery异步加载分部视图,Home/Index.cshtml:
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h4>Index</h4> <div> <a href="#">通过jQuery异步</a> <br/> </div> <div> </div> @section scripts { <script type="text/javascript"> $(function() { $('#a').click(function() { $.ajax({ url: '@Url.Action("Index","Home")', data: { pre: 'B' }, type: 'POST', success: function(data) { $('#result').empty().append(data); } }); return false; }); }); </script> }HomeController控制器中:
using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using MvcApplication1.Models; namespace MvcApplication1.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(string pre) { var result = GetAllTeams().Where(t => t.Preletter == pre).ToList(); ViewBag.msg = "通过jQuery异步方式到达这里~~"; return PartialView("TeamY", result); } private List<Team> GetAllTeams() { return new List<Team>() { new Team(){Name = "巴西队", Preletter = "B"}, new Team(){Name = "克罗地亚队", Preletter = "K"}, new Team(){Name = "巴拉圭", Preletter = "B"}, new Team(){Name = "韩国", Preletter = "K"} }; } } }分部视图TeamY.cshtml:
@model IEnumerable<MvcApplication1.Models.Team> @{ var result = string.Empty; foreach (var item in Model) { result += item.Name + ","; } } @ViewBag.msg.ToString() <br/> @result.Substring(0,result.Length - 1)3、基于JQuery的表单异步提交
举个栗子吧:
<script type="text/javascript"> $(document).ready(function () { $("#form1").submit(function (event) { event.preventDefault();//阻止默认提交事件,改用JS处理提交事件$.ajax({
type:"Post//表单提交类型
url: "@Url.Content("~/User/Create")",//表单提交的Action方法
data:$("#form1").serialize(), //序列化表单的值为字符串,前提是表单里边的输入标签都要有name属性才可以,序列化后的形式大概是这样的:a=1&b=2&c=3&d=4&e=5
success:function(msg){
$("#result").html(msg);
}
});
return false;
});
}); </script>
但是我觉得如果表单提交的数据少的话,可以用这种,如果多的话,就没有必要了,用MVC自带的更好
三、如何提高Ajax性能
1、适当使用缓存机制
2、使用CDN内容分发来访问Jquery脚本:
(1)自己公司架设CDN服务器
(2)使用第三方公司的,比如微软,谷歌等公司的CDN,但有时候不太靠谱
3、JS/CSS文件的打包合并(Bundling)及压缩(Minification)