<h3>MVC 自带的yibu请求</h3>
<%-- 1.要执行的方法,2.控制器,3.Ajax操作--%>
<%using (Ajax.BeginForm("Date", "Ajax", new AjaxOptions() {
Confirm="确认要提交吗",
HttpMethod="post",
UpdateTargetId = "ResultDiv",
InsertionMode=InsertionMode.InsertAfter,
OnSuccess="AfterSuccess",
LoadingElementId = "UpdateTargetId",
}))
{ %>
<input name="TxtName" />
<br />
<input type="submit" value="获取日期" />
<%} %>
<div id="ResultDiv">
</div>
<div id="UpdateTargetId">
请稍等,正在与后台交互中,Load...........<br/>
UpdateTargetId = "UpdateTargetId"
</div>
至关重要的是Scripts/jquery.unobtrusive-ajax.js这个js文件 是unobtrusive隐式的意思
在这个例子中,只有一个按钮,异步请求也就是当有数据提交时(本页面有按钮被按下时)都会进行异步提交,都会触发异步里写的方法属性
----------------------------------------------------------------------------------------------------------
翻译成源代码:
<form action="/Ajax/Date" data-ajax="true" data-ajax-confirm="确认要提交吗" data-ajax-loading="#UpdateTargetId" data-ajax-method="post" data-ajax-mode="after" data-ajax-success="AfterSuccess" data-ajax-update="#ResultDiv" id="form0" method="post">
<input name="TxtName" />
<br />
<input type="submit" value="获取日期" />
</form>
<div id="ResultDiv">
</div>
<div id="UpdateTargetId">
请稍等,正在与后台交互中,Load...........<br/>
</div>
----------------------------------------------------------------------------------------------------------
public ActionResult Index()
{
return View();
}
public ActionResult Date()
{
Thread.Sleep(5000);
return Content(DateTime.Now.ToString());
}