博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
爱上MVC3系列~Html.BeginForm与Ajax.BeginForm
阅读量:6853 次
发布时间:2019-06-26

本文共 2978 字,大约阅读时间需要 9 分钟。

Html.BeginForm与Ajax.BeginForm都是MVC架构中的表单元素,它们从字面上可以看到区别,即Html.BeginForm是普通的表单提交,而Ajax.BeginForm是支持异步的表单提交,这对于我们开发者来说是一个福音,我们不用再自己去用JQ代码了,直接用MVC自代的Ajax.BeginForm就可以很容易的完成一个异步的表单提交动作。

Html.BeginForm的原型解释:

1 @using (Html.BeginForm()) {} //提交到当前页面 2  3 @using (Html.BeginForm(new {} )) {} //提交到当前页面,并可以传递参数 4  5 @using (Html.BeginForm("action","controller")) {} //提交到指定controller下的action中 6  7 @using (Html.BeginForm("action","controller",FormMethod.POST)) {} //提交到指定controller下的action中,并指定提交方式 8  9 FormMethod枚举如下:   10 11  // 摘要:12     //     枚举窗体的 HTTP 请求类型。13     public enum FormMethod14     {15         // 摘要:16         //     指定 GET 请求。17         Get = 0,18         //19         // 摘要:20         //     指定 POST 请求。21         Post = 1,22     }

 

Ajax.BeginForm异步表单原型解释

1 @using (Ajax.BeginForm( 2     new AjaxOptions 3     { 4         UpdateTargetId = "UserLogOnContainer", 5         HttpMethod = "Post", 6         OnSuccess = " ", 7     })){} //提交到当前页面,提交方式为Post,异步更新模块ID为UserLogOnContainer 8  9  @using (Ajax.BeginForm("action", "controller", null,10     new AjaxOptions11     {12         UpdateTargetId = "UserLogOnContainer",13         HttpMethod = "Post",14         OnSuccess = " ",15     }))16     {} //提交到指定controller下的action,提交方式为Post,异步更新模块ID为UserLogOnContainer

 

下面看一下Ajax.BeginForm的例子,一个用户登陆的DEMO

View代码:

1 @model TsingDa.Ask.Models.UserLogOnModel 2 @{Layout = "";} 3  4  5  6 
7 @using (Ajax.BeginForm("UserLogOn", "Home", null, 8 new AjaxOptions 9 {10 UpdateTargetId = "UserLogOnContainer",11 HttpMethod = "Post",12 OnSuccess = " ",13 }))14 {15 @Html.ValidationSummary(true)16
17 @Html.TextBoxFor(m => m.Email)18 @Html.ValidationMessageFor(m => m.Email)19
20
21 @Html.TextBoxFor(m => m.Password)22 @Html.ValidationMessageFor(m => m.Password)23
24
25 }26

Controller层代码如下:

1      ///  2         /// 用户登陆 3         ///  4         /// 
5 public ActionResult UserLogOn() 6 { 7 return View(new UserLogOnModel("邮箱", "密码")); 8 } 9 [HttpPost]10 public ActionResult UserLogOn(UserLogOnModel entity)11 {12 if (ModelState.IsValid)13 {14 VM = user_InfoManager.UserLogOn(new User_Info { Email = entity.Email, Password = entity.Password });15 if (VM.IsComplete)16 {17 return RedirectToAction("Index", "Home");18 }19 else20 {21 VM.ToList().ForEach(i => ModelState.AddModelError("", i));22 }23 }24 25 return View();26 }

表单提交后,页面效果如下:

需要注意的是,表单中的按钮在异步表单中也是Submit类型,如果是异步表单,引入的JS文件需要有jquery.unobtrusive-ajax.min.js,在这项目的scripts目录已经存在。

 

Reference from : 

转载于:https://www.cnblogs.com/zhangchenliang/p/4114849.html

你可能感兴趣的文章
使用MEF+MVVM light+WCF RIA Service实现的Sellthrough系统
查看>>
it技能图
查看>>
jedis 2.0.0 for maven usage
查看>>
curl+sed+shell编写一个英语翻译脚本
查看>>
C Array length function problem - C / C++
查看>>
ASP.NET中26个常用性能优化方法
查看>>
Objective-C利用协议实现回调函数
查看>>
【021】VS2010实现强类型DataSet
查看>>
sqlserver 各种判断是否存在(表名、函数、存储过程.......)
查看>>
使用mssql2008新特性(存储过程参数类型使用"用户自定义表"来实现批量DML更新多表)解决项目里遇到的性能问题...
查看>>
[置顶] 深入浅出Spring(三) AOP详解
查看>>
设计模式—策略模式
查看>>
Subset II leetcode java
查看>>
目标检测之视频摘要---即视频浓缩,视频检索,视频摘要浓缩
查看>>
Linux下解压tar.xz
查看>>
Linux addr2line命令
查看>>
Java集合之HashSet源码分析
查看>>
自定义圆形控件 RoundImageView
查看>>
CreateProcessAsUser,C#写的windows服务弹框提示消息或者启动子进程
查看>>
JAXB简单样例
查看>>