MVC4 简单异步演示
2015-06-12 15:55
218 查看
MVC中的异步提交有两种方法,一种是JQuery方式,一种是MVC中自带的Ajax.BeginForm方式,两种都可以实现异步提交与刷新
JQuery.Ajax
Controllers代码
public ActionResult Index() { return View(); } public ActionResult GetDate() { //获取参数信息 string id = Request["id"].ToString(); string name = Request["name"].ToString(); return Content("用户:"+name+" 时间:"+ DateTime.Now.ToString()); }
View页面代码
@{ ViewBag.Title = "Index"; } <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <script src="~/Scripts/jquery-1.8.2.js"></script> @*使用JQuery的异步方式获取后台的信息*@ <script type="text/javascript"> $(function () { $("#btnJQ").click(function () { //从后台获取时间 $.ajax({ url: "/Ajax/GetDate",//请求地址 type: "Get",//请求的类型 success: function (data) { //成功后的回调函数 alert(data); }, data: "id=1&name=信息"//传递的数据 }); }); }); </script> </head> <body> <h2>Ajax页面</h2> <div> <input type="button" id="btnJQ" value="显示后台返回的数据" /> </div> </body> </html>
Ajax.BeginForm
Controllers代码
public ActionResult MicrosoftAjax() { return View(); } public ActionResult ReData() { //获取参数信息 string sname = Request["UserName"].ToString(); //让网站睡眠1秒钟 //System.Threading.Thread.Sleep(1000); return Content("用户名:" + sname); }
View页面代码
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>MicrosoftAjax</title> <script src="~/Scripts/jquery-1.8.2.js"></script> <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script> <script type="text/javascript"> function afterSuccess(data) { //数据执行成功后,触发的事件 alert(data); } </script> </head> <body> <div> @using (Ajax.BeginForm("ReData", "Ajax", new AjaxOptions() { Confirm = "您是否要提交吗?", HttpMethod = "Post", InsertionMode = InsertionMode.Replace, UpdateTargetId = "result", OnSuccess = "afterSuccess" })) { <div> 用户名:<input type="text" name="UserName" /><br /> 密码:<input type="text" name="Pwd" /><br /> <input type="submit" value="提交" /> </div> } <div id="result"> 这个是要显示结果的div </div> </div> </body> </html>
相关文章推荐
- nginx.conf文件
- Linux下查看和清空route cache
- JSP之九大隐藏对象
- Java 线程同步
- HTML页面表单输入框去掉鼠标选中后边框变色的效果
- 关于if简写语句优化的方法
- Leetcode 139 Word Break
- epoll和select区别
- 九度OJ-题目1354:和为S的连续正数序列
- DataTable和 DataRow的 区别与联系
- 笔记处
- clonezilla 批量装机
- poj3630
- 对宏定义中出现的##运算符和#运算符的说明
- java执行http请求乱码解决
- NSMutableArray 删除元素
- 浅谈JAVA反射机制在Android应用开发中的应用
- 关于spring的介绍
- udp tcp
- WebEssentials 在vs2013 update5安装报错的解决方法.