A.首先创建basic类型的项目.
B.创建HomeController.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcAjaxTest.Controllers
{
public class HomeController : Controller
{
//
// GET: /Homw/
public ActionResult Index()
{
return View();
}
[OutputCache(NoStore=true,Duration=0)]
public ActionResult GetTime()
{
return Content(DateTime.Now.ToString("F"));
}
}
}
C.创建相应的View文件:
Index.cshtml:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<h2>测试1: @Html.ActionLink("利用链接的老技术Get方法获得当前服务器时间","GetTime","Home")</h2>
<hr />
<h2>测试2: @Ajax.ActionLink("利用链接的Ajax技术的Get方法获得当前服务器时间","GetTime","Home",new AjaxOptions{UpdateTargetId="now1",Confirm="确定读取服务器时间吗?",HttpMethod="get"})</h2>
<div id="now1"></div>
<hr />
@using (Ajax.BeginForm("GetTime", "Home", new AjaxOptions {UpdateTargetId="now2",InsertionMode = InsertionMode.InsertAfter, HttpMethod = "Post", OnSuccess= "tip"}))
{
<input type="submit" value="测试3: 利用表单的Ajax技术的Post方法获得当前服务器时间" />
}
<div id="now2"></div>
<hr />
<div id="tip"></div>
@section scripts
{
@*<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")"></script>*@
@Scripts.Render("~/bundles/jqueryval")
<script language="javascript" type="text/javascript">
function tip(data)
{
alert("Sucess");
$("#tip").after("<i>"+data+"</i><br/>");
}
</script>
}
重点:
1.view文件中引入Script相关的多种方法.
2.MVC中采用Ajax的专用多种书写方式.
AspNet MVC4 教学-21:Asp.Net MVC4 Ajax技术快速应用Demo
原文地址:http://blog.csdn.net/vinglemar/article/details/46290117