描述this GitHUb branch的工作原理:
首先,让我们定义一个我们要请求的操作。为简单起见,我们只做一个非常基本的 POST 操作:
//
// POST: /Home/Ajax
[HttpPost]
public PartialViewResult Ajax()
{
// use partial view so we're not bringing the entire page's theme
// back in the response. We're simply returning the content within
// ~/Views/Home/Ajax.cshtml
return PartialView();
}
接下来,为您的内容设置一个目的地并为其指定一个 id(这里我将其命名为“update-me”):
<div class="well" id="update-me">
Click the button to see some AJAX content.
</div>
从那里开始,我们设置表单。下面演示了标准的 AJAX 功能,但您可以将自己的函数绑定到 AjaxOptions 指定的一些事件。
@using (Ajax.BeginForm("Ajax", new AjaxOptions {
HttpMethod = "POST", // HttpPost
InsertionMode = InsertionMode.Replace, // empty the target first
UpdateTargetId = "update-me" // place content within #update-me
}))
{
<button type="submit" class="btn btn-default">
<i class="glyphicon glyphicon-refresh"></i>
Click Me!
</button>
}
最后,我们需要指定负责表单功能的大部分[“自动”]连接的脚本库:
<script src="~/Scripts/jquery-2.1.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
就是这样。当您开始使用它时,您会发现扩展它非常简单。例如,如果您想显示“工作”图标,您可以在 OnBegin 和 OnComplete 属性中指定自定义函数。