在ASP.NET MVC视图中通过 @using (Html.BeginForm()) 产生的是form表单提交代码,可以用javascript代码截获这个form提交,改为ajax提交,示例代码如下:

代码来自:ASP.NET MVC 3 Unobtrusive Javascript Validation With Custom Validators

$('#form1').submit(function () {
if ($(this).valid()) {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
$('#result').html(result);
},
error: function (result) {
alert(result);
}
});
}
return false;
});

用ASP.NET MVC自带的Ajax.BeginForm也可以实现Ajax提交,但对返回结果的控制没有上面的方法灵活,代码如下:

@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "result" }))



相关文章:

  • 2022-02-05
  • 2021-09-19
  • 2022-12-23
  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
  • 2022-02-21
  • 2021-08-16
猜你喜欢
  • 2021-12-17
  • 2022-01-10
  • 2022-12-23
  • 2022-02-18
  • 2021-09-20
  • 2022-12-23
相关资源
相似解决方案