标签:
1.利用MVC自带的方法可实现无刷新请求,其原理就是封装了JS的AJAX
实现代码如下:
前端代码:
@using (Ajax.BeginForm("Index", "Home", new { ID=1,Name="2"}, new AjaxOptions
{
HttpMethod="POST",
InsertionMode=InsertionMode.Replace,
OnBegin = "OnBeginPost()",
OnComplete = "OnEndPost()",
OnSuccess = "OnSuccessPost",
UpdateTargetId = "content"
}
))
{
<div id="content">
<table>
<tr>
<td>
@Html.Label("lblName", "姓名")
</td>
<td>
@Html.TextBox("TxtName")
</td>
</tr>
<tr>
<td>
@Html.Label("lblAge", "年龄")
</td>
<td>
@Html.TextBox("TxtAge")
</td>
</tr>
</table>
<input type="submit" value="提交" />
</div>
}
JS代码
<script type="text/javascript">
function OnBeginPost() {
alert("开始提交");
}
function OnEndPost() {
alert("提交完成!");
}
function OnSuccessPost(e) {
alert(e + "提交成功!");
}
</script>
}
说明一点利用此方法需要引用JS文件 <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
否则实现不了上述功能
MVC-Ajax.BeginForm的用法
标签:
原文地址:http://www.cnblogs.com/dwtMyBlog521/p/4378666.html