1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 7 namespace MvcValidateDemo.Controllers 8 { 9 public class AjaxController : Controller 10 { 11 // 12 // GET: /Ajax/ 13 14 public ActionResult Index() 15 { 16 return View(); 17 } 18 19 public ActionResult GetDate() 20 { 21 //让网站睡眠1秒钟 22 System.Threading.Thread.Sleep(1000); 23 return Content(DateTime.Now.ToString()); 24 } 25 26 public ActionResult MicrosoftAjax() 27 { 28 return View(); 29 } 30 31 } 32 }
1 @{ 2 Layout = null; 3 } 4 5 <!DOCTYPE html> 6 7 <html> 8 <head> 9 <meta name="viewport" content="width=device-width" /> 10 <title>MicrosoftAjax</title> 11 <script src="~/Scripts/jquery-1.8.2.js"></script> 12 <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script> 13 <script type="text/javascript"> 14 function afterSuccess(data) { 15 //alert(data); 16 } 17 </script> 18 </head> 19 <body> 20 <div> 21 @using (Ajax.BeginForm("GetDate", "Ajax", new AjaxOptions(){Confirm ="您是否要提交吗?",HttpMethod = "Post",InsertionMode = InsertionMode.InsertAfter,UpdateTargetId = "result",OnSuccess="afterSuccess",LoadingElementId = "loading"})) 22 { 23 <div> 24 用户名:<input type="text" name="UserName" /><br /> 25 密码:<input type="text" name="Pwd"/><br /> 26 <input type="submit" value="提交"/> 27 </div> 28 } 29 30 <div id="result"> 31 32 </div> 33 34 <div id="loading" style="display:none"> 35 <img src="~/Content/ico_loading2.gif" /> 36 </div> 37 </div> 38 </body> 39 </html>