`

将ASP.NET MVC中的form提交改为ajax提交

 
阅读更多

在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" }))

 

分享到:
评论

相关推荐

    ASP.NET MVC 4 Web编程

    第1章:ASP.NET MVC基础 第2章:ASP.NET Web Form开发人员必读 第3章:使用数据 第4章:客户端开发 第5章:Web应用程序架构 第6章:使用AJAX提升网站体验 第7章:ASP.NET Web API 第8章:高级数据 第9章:安全 第10...

    ASP.NET MVC下Ajax.BeginForm方式无刷新提交表单实例

    下面小编就为大家分享一篇ASP.NET MVC下Ajax.BeginForm方式无刷新提交表单实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    asp.net mvc

    To help reduce conflicts with other Ajax libraries, the built-in ASP.NET MVC validation scripts are now in a separate JavaScript file. ASP.NET MVC validation scripts can be included at the top or ...

    Professional ASP.NET MVC 1.0原版第三部分

    This book begins with you working along as Scott Guthrie builds a complete ASP.NET MVC reference application. He begins NerdDinner by using the File-New Project menu command within Visual Studio to ...

    Professional ASP.NET MVC 1.0 原版第二部分

    This book begins with you working along as Scott Guthrie builds a complete ASP.NET MVC reference application. He begins NerdDinner by using the File-New Project menu command within Visual Studio to ...

    Professional ASP.NET MVC 1.0原版第四部分

    This book begins with you working along as Scott Guthrie builds a complete ASP.NET MVC reference application. He begins NerdDinner by using the File-New Project menu command within Visual Studio to ...

    Professional ASP.NET MVC 1.0原版第五部分

    This book begins with you working along as Scott Guthrie builds a complete ASP.NET MVC reference application. He begins NerdDinner by using the File-New Project menu command within Visual Studio to ...

    Professional ASP.NET MVC 1.0原版第六部分

    This book begins with you working along as Scott Guthrie builds a complete ASP.NET MVC reference application. He begins NerdDinner by using the File-New Project menu command within Visual Studio to ...

    Professional ASP.NET MVC 1.0原版第一部分

    This book begins with you working along as Scott Guthrie builds a complete ASP.NET MVC reference application. He begins NerdDinner by using the File->New Project menu command within Visual Studio to ...

    asp.net mvc 实现文件上传带进度条的思路与方法

    文件上传与下载的操作在实际项目中经常是很重要的一个内容,在使用ASP.NET Web Form的时候我们可以使用诸多的服务器控件,FileIpload就是其中之一,但是在ASP.NET不建议使用那些服务器控件,因为那样违反三层架构...

    浅析Asp.net MVC 中Ajax的使用

    一、使用System.Web.Mvc.Ajax  1.1 System.Web.Mvc.Ajax.BeginForm  1.2 System.Web.Mvc.Ajax.ActionLink 二、手工打造自己的“非介入式”Javascript” 一、使用System.Web.Mvc.Ajax   1.1 System.Web.Mvc.Ajax....

    ASP.NET MVC Framework Unleashed

    Part IBuilding ASP.NET MVC Applications 5 1 An Introduction to ASP.NET MVC.................................................................7 2 Building a Simple ASP.NET MVC Application...................

    如何使用Ajax使用实体框架在ASP.NET mvc5中通过下拉列表填充表单字段

    首先,“无效”对我们没有任何... 您的ajax代码可以运行吗? 它会调用您的方法吗? 你得到错误了吗? 该方法是否返回数据? 您的函数是否获取数据? 成功事件是否运行? 是否可以找到您的元素? 它能找到想要的东西吗?

    MVC4web编程

    ASP.NET MVC 4 Web编程 目录: 第1章:ASP.NET MVC基础 第2章:ASP.NET Web Form开发人员必读 第3章:使用数据 第4章:客户端开发 第5章:Web应用程序架构 第6章:使用AJAX提升网站...附录A ASP.NET MVC与Web Form集成

    ASP.NET3.5从入门到精通

    1.1.6 ASP.NET 3.5 AJAX 1.2 .NET 应用程序需框架 1.2.1 什么是.NET 应用程序框架 1.2.2 公共语言运行时(CLR) 1.2.3 .NET Framework 类库 1.3 安装Visual Studio 2008 1.3.1 安装Visual Studio 2008 1.3.2 主窗口 ...

    ASP.NET 3.5 开发大全

    1.1.6 ASP.NET 3.5 AJAX 1.2 .NET应用程序需框架 1.2.1 什么是.NET应用程序框架 1.2.2 公共语言运行时(CLR) 1.2.3 .NET Framework 类库 1.3 安装Visual Studio 2008 1.3.1 安装Visual Studio 2008 1.3.2 主窗口 ...

    ASP.NET 3.5 开发大全word课件

    这是整部学习资料 由于太大第一章免费供应给大家 在我的上传资源中 如果觉得还不过希望大家给个好评 当然具体本书的作者就不深究了把! 第1章 认识ASP.NET 3.5 ...17.4.5 ASP.NET MVC URL路由(URLRouting)...

    ASP.NET 3.5 开发大全11-15

    1.1.6 ASP.NET 3.5 AJAX 1.2 .NET应用程序需框架 1.2.1 什么是.NET应用程序框架 1.2.2 公共语言运行时(CLR) 1.2.3 .NET Framework 类库 1.3 安装Visual Studio 2008 1.3.1 安装Visual Studio 2008 1.3.2 主窗口 ...

    asp.net知识库

    在Asp.net中如何用SQLDMO来获取SQL Server中的对象信息 使用Relations建立表之间的关系并却使用PagedDataSource类对DataList进行分页 通过作业,定时同步两个数据库 SQLSERVER高级注入技巧 利用反射实现ASP.NET控件和...

Global site tag (gtag.js) - Google Analytics