MVC 使用Jquery实现AJax
来源:互联网 发布:50岁知天命 编辑:程序博客网 时间:2024/05/19 00:11
来自:http://www.cnblogs.com/celery94/archive/2011/01/17/1937494.html
在上一篇介绍MVC中的Ajax实现方法的时候,曾经提到了除了使用Ajax HTML Helper方式来实现之外,Jquery也是实现Ajax的另外一种方案。
通过get方法实现AJax请求
View
<script type="text/javascript"> function GetTime() { $.get("Home/GetTime", function (response) { $("#myPnl").html(response); }); return false; }</script><div id="myPnl" style="width: 300px; height: 30px; border: 1px dotted silver;"></div><a href="#" onclick="return GetTime();">Click Me</a>
Controller
public ActionResult GetTime(){ return Content(DateTime.Now.ToString());}
通过post方法实现Form的Ajax提交
View
@model MvcAjax.Models.UserModel@{ ViewBag.Title = "AjaxForm";}<script type="text/javascript"> $(document).ready(function () { $("form[action$='SaveUser']").submit(function () { $.post($(this).attr("action"), $(this).serialize(), function (response) { $("#myPnl").html(response); }); return false; }); }); </script><div id="myPnl" style="width: 300px; height: 30px; border: 1px dotted silver;"></div>@using (Html.BeginForm("SaveUser", "Home")){ <table> <tr> <td> @Html.LabelFor(m => m.UserName) </td> <td> @Html.TextBoxFor(m => m.UserName) </td> </tr> <tr> <td> @Html.LabelFor(m => m.Email) </td> <td> @Html.TextBoxFor(m => m.Email) </td> </tr> <tr> <td> @Html.LabelFor(m => m.Desc) </td> <td> @Html.TextBoxFor(m => m.Desc) </td> </tr> <tr> <td colspan="2"> <input type="submit" value="Submit" /> </td> </tr> </table>}
Model
using System.ComponentModel.DataAnnotations;namespace MvcAjax.Models{ public class UserModel { [Display(Name = "Username")] public string UserName { get; set; } [Display(Name = "Email")] public string Email { get; set; } [Display(Name = "Description")] public string Desc { get; set; } }}
Controller
[HttpPost]public ActionResult SaveUser(UserModel userModel){ //Save User Code Here //...... return Content("Save Complete!");}
以上代码实现了Jquery POST提交数据的方法。
通过查看以上两种Jquery方式的Ajax实现方法,和之前AJax HTML Helper比较可以发现其实Controller都是相同的。
采用Jquery方式提交数据的的主要实现方案就是通过Jquery的get或者post方法,发送请求到MVC的controller中,然后处理获取的response,更新到页面中。
注意点:
无论是使用超链接和form方式来提交请求,javascript的方法始终都有一个返回值false,用来防止超链接的跳转或者是form的实际提交。
这个地方就会出现一个疑问:
如果针对该页面禁止了Javascript脚本,那么该页面就是跳转或者是form就是提交,返回的ActionResult处理就会出现问题了。
这个时候就需要在Controller中判断该请求是否是Ajax请求,根据不同的情况返回不同的ActionResult:
[HttpPost]public ActionResult SaveUser(UserModel userModel){ //Save User Code Here //...... if (Request.IsAjaxRequest()) return Content("Save Complete!"); else return View();}
- MVC 使用Jquery实现AJax
- MVC 使用Jquery实现AJax
- Asp.Net Mvc 使用jQuery实现Ajax文件上传
- ASP.NET MVC 使用Jquery Ajax实现登录
- .net mvc使用jquery ajax
- 使用jQuery实现Ajax
- 使用jQuery实现ajax
- 使用 jQuery 实现 Ajax
- 使用jQuery实现Ajax
- Spring MVC 结合jQuery实现AJAX
- .net MVC中Jquery实现AJAX详解
- asp.net MVC 使用JQuery.Ajax
- asp.net MVC 使用JQuery.Ajax
- asp.net MVC 使用JQuery.Ajax
- 使用jQuery轻松实现Ajax
- 10003---使用jQuery实现Ajax
- 浅谈使用jQuery实现Ajax
- Ajax-02-使用jQuery实现
- 推荐的论文
- 使用Jacob合并Word文档并填充文档中的书签
- Spring 集成 Quartz 配置以及例子
- 《JAVA与模式》26天系列—第3天—抽象工厂模式
- python文件管理中的file、seek、truncate
- MVC 使用Jquery实现AJax
- 释放SQL Server占用的内存
- C++ Boost Thread 编程指南
- DataTable多表合并 同结构和不同结构的合并
- DevCache轻量级缓存组件
- F-dd-vctools-vc7libs-ship-atlmfc-src-mfc-doctempl-cpp
- NSMutableAttributedString计算内容长度
- python读取文件末尾N行
- 自己编写错误代码的规范