在MVC中要实现Ajax有很多的方式,有微软自己的MicrosoftAjax,也可以用JQuery的AJax来实现,如果对其他的JavaScript框架熟悉,还可以采用其他的实现方案,比如说Prototype等等。

以下是微软自己的实现方案。

需要预先加载的JavaScript文件:

    <script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>

在MVC中已经提供了下面几个现成的HTML Hepler:

  • Ajax.ActionLink()
  • Ajax.BeginForm()
  • Ajax.RouteLink()
  • Ajax.BeginRouteForm()

Ajax.ActionLink

使用ActionLink发送异步请求的方法:

View

<div id="myPnl" style="width: 300px; height: 30px; border: 1px dotted silver;">
</div>
@Ajax.ActionLink("Click Me", "GetTime", new AjaxOptions { UpdateTargetId = "myPnl" })

Controller

public ActionResult GetTime()
{
return Content(DateTime.Now.ToString());
}

以上示例使用ActionLink超链接发送请求到GetTime,返回一个ContentResult,通过AjaxOptions中的UpdateTargetId属性指定了需要更新的页面元素。

AjaxOptions中还有其他可以指定的属性:

Confirm
等效于javascript中的return confirm(msg),在点击该链接时先提示需要确认的信息。

HttpMethod

指定使用Get或者是Post方式发送Http请求

InsertMode

指定使用哪一种方式在指定的UpdateTargetId元素更新数据,可以有三种方式: "InsertAfter", "InsertBefore", or "Replace" 。默认为:Replace

LoadingElementDuration

Loading元素显示的时间

LoadingElementId

可以指定在Http请求期间显示的Loading元素

OnBegin

在Http请求之前执行的javascript方法

OnComplete

在Http请求结束时执行的方法

OnFailure

在Http请求失败时执行的方法

OnSuccess

在Http请求成功时执行的方法

UpdateTargetId

Http请求更新的页面元素

Url

Http请求的Url

关于AjaxOptions中各方法的使用方法,在之前关于ActionResult的介绍的文章中有相关的列子:

JsonResult

注意点

  • OnComplete和OnSuccess的区别:OnComplete是获取了Http请求时引发的,此时页面还没有进行更新,OnSuccess是在页面已经更新后引发的。
  • ActionLink中的actionName和AjaxOption中的Url的关系:两者分别产生的HTML如下,但是执行的结果相同,希望有高手能解释下这两者有无区别。

<a href="/Home/GetTime" data-ajax-update="#myPnl" data-ajax-mode="replace" data-ajax="true">Click Me</a>

<a href="/" data-ajax-url="Home/GetTime" data-ajax-update="#myPnl" data-ajax-mode="replace" data-ajax="true">Click Me</a>

Ajax.BeginForm

该Html Hepler可以实现使用Ajax方式提交Form,在指定的页面元素中显示提交的结果。

View

@model MvcAjax.Models.UserModel
@{
ViewBag.Title = "AjaxForm";
} <div id="myPnl" style="width: 300px; height: 30px; border: 1px dotted silver;">
</div> @using (Ajax.BeginForm("SaveUser", new AjaxOptions { UpdateTargetId = "myPnl" }))
{
<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

public ActionResult AjaxForm()
{
return View();
} [HttpPost]
public ActionResult SaveUser(UserModel userModel)
{
//Save User Code Here
//...... return Content("Save Complete!");
}

以上示例代码实现了采用Ajax提交Form数据的大概方法,在Ajax.BeginForm中同样使用AjaxOptions来设置Ajax请求的参数,和Ajax.ActionLink中的使用方法相同。

其他:

介绍JavaScriptResult时曾经提到了该ActionResult在普通的请求中是直接当作文件Reponse出的,但是在Ajax请求中,便可以使用该Result,并且执行Result中的JavaScript。

比如将上面的Conntroller更改为以下代码:

[HttpPost]
public ActionResult SaveUser(UserModel userModel)
{
//Save User Code Here
//...... //return Content("Save Complete!");
return JavaScript("alert('Save Complete!');");
}

便可在执行改Ajax请求之后执行JavaScriptResult中的语句。

最新文章

  1. jQuery如何在IE中更改网页标题
  2. [转]使用Stopwatch类实现高精度计时
  3. CxImage在VS2010下的配置
  4. I2C学习
  5. 【7集iCore3基础视频】7-3 iCore3硬件介绍
  6. 【转】前端精选文摘:BFC 神奇背后的原理
  7. Shell编程中Shift的用法
  8. 【BZOJ】1085: [SCOI2005]骑士精神(A*启发式搜索)
  9. Android Studio系列教程一--下载与安装
  10. fiddler 插件开发二
  11. 面向对象基础3(class0523)
  12. 如何对Site Settings页面进行定制化 添加一个setting 链接
  13. 【Spark2.0源码学习】-10.Task执行与回馈
  14. NowCoder牛客练习赛7-A.骰子的游戏 B.购物-优先队列
  15. Linux下键盘值 对应input_evnet的code值。
  16. Multi-Targeting and Porting a .NET Library to .NET Core 2.0
  17. R语言之正则表达式
  18. BZOJ4894:天赋(矩阵树定理)
  19. [转载]嵌入式C语言中的Doxygen注释模板
  20. Android笔记(二):savedIndstanceState 和 Bundle

热门文章

  1. hdu 5510 Bazinga
  2. JPA多对多@manytomany注解配置实例
  3. C++视频课程小结(2)
  4. LPTSTR、LPCSTR、LPCTSTR、LPSTR的来源及意义
  5. junit4学习(Annotation)
  6. Linux内核完全注释之编程语言和环境(二)
  7. IOS深入学习(19)之View object
  8. 利用HTML5开发Android(4)---HTML5本地存储之Web Storage
  9. [转]C/C++中的memset
  10. [翻译]比较ADO.NET中的不同数据访问技术(Performance Comparison:Data Access Techniques)