Html.BeginForm() vs Ajax.BeginForm() in MVC3
我们知道,BeginForm()方法能创建一个Form标签,因此可以结合表单级的方法,在这个页面中。我一直在考虑Html.BeginForm()方法和Ajax.BeginForm()方法在MVC3中有什么不同。读了很多博客,很多人都强调了一件事:Ajax Form,Form被提交是使用了JavaScript异步提交的。
Html Form和 Ajax Form区别:
一,我做了一个简单的Demo来示范:
Step1:创建一个MVC项目
Step2:创建一个视图名为TestHtmlView.cshtml,此视图的Form表单使用Html.BeginForm()创建。此例子的操作是:当提交此表单时进行重定向;
- <span style="font-family:KaiTi_GB2312;font-size:18px;">@{
- ViewBag.Title = "Home Page";
- }
- <h2>@ViewBag.Message</h2>
- <p>
- @using(Html.BeginForm("TestHtmlRedirect", "Test",FormMethod.Post, null))
- {
- <input type="submit"value="Html PsBk Click" />
- }
- </p></span>
Step3:定义两个action方法,一个用于返回创建的视图,一个用于响应表单提交;
- <span style="font-family:KaiTi_GB2312;font-size:18px;"> //This section of code is forTestHtmlView.cshtml
- public ActionResult TestHtmlView()
- {
- ViewBag.Message = "HtmlForm——This is a HTML form";
- return View();
- }
- [HttpPost]
- public ActionResult TestHtmlRedirect()
- {
- returnRedirectToAction("TestAjaxView", "Test", null);
- }
- //End of the section of code forTestHtmlView.cshtml</span>
看一下TestHtmlRedirect()方法的实现体,我们想从该视图重定向到另一个视图TestAjaxView.cshtml。
Step4:创建一个视图名为AjaxHtmlView.cshtml,此视图的Form表单使用Ajax.BeginForm()创建。
- <span style="font-family:KaiTi_GB2312;font-size:18px;">@{
- ViewBag.Title = "Test Page";
- }
- <scriptsrcscriptsrc="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"type="text/javascript"></script>
- <h2>@ViewBag.Message</h2>
- <p>
- @using(Ajax.BeginForm("TestAjaxRedirect", "Test",FormMethod.Post, null))
- {
- <input type="submit"value="Ajax PsBk Click" />
- }
- </p></span>
Step5:如果想让此Ajax Form正确工作,能达到预期,那么还需要在AjaxHtmlView.cshtml中添加此JS文件引用
<scriptsrc="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"type="text/javascript"></script>
还要确保在Web.Config文件中支持JS的执行,需要此配置文件中添加如下标签:
- <span style="font-family:KaiTi_GB2312;font-size:18px;"> <!--启用客户端验证,Start。。。-->
- <addkeyaddkey="ClientValidationEnabled" value="true"/>
- <!--支持JavaScript的执行-->
- <addkeyaddkey="UnobtrusiveJavaScriptEnabled" value="true"/></span>
Step6:定义两个action方法,一个用于返回创建的视图,一个用于响应表单提交;
- <span style="font-family:KaiTi_GB2312;font-size:18px;">//This section ofcode is for TestAjaxView.cshtml
- public ActionResult TestAjaxView()
- {
- ViewBag.Message = "AjaxForm——This is a AJAX form";
- return View();
- }
- [HttpPost]
- public ActionResult TestAjaxRedirect()
- {
- returnRedirectToAction("About", "Test", null);
- }
- //End of Section of code forTestAjaxView.cshtml</span>
看一下TestAjaxRedirect()方法的实现体,我们想从该视图重定向到另一个视图About.cshtml。
(附录:
(1)About.cshtml:
- <span style="font-family:KaiTi_GB2312;font-size:18px;">@{
- ViewBag.Title = "关于我们";
- }
- <h2>关于</h2>
- <p>
- 将内容放置在此处。
- </p></span>
(2)Test控制器中添加About方法:
- <span style="font-family:KaiTi_GB2312;font-size:18px;">public ActionResult About() {
- return View();
- }</span>
(3)Global.asax
- <span style="font-family:KaiTi_GB2312;font-size:18px;">using System;
- usingSystem.Collections.Generic;
- using System.Linq;
- using System.Web;
- usingSystem.Web.Mvc;
- usingSystem.Web.Routing;
- namespaceComplaintManageSystem
- {
- // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
- // 请访问 http://go.microsoft.com/?LinkId=9394801
- public class MvcApplication :System.Web.HttpApplication
- {
- public static voidRegisterGlobalFilters(GlobalFilterCollection filters)
- {
- filters.Add(newHandleErrorAttribute());
- }
- public static voidRegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute(
- "Default", // 路由名称
- "{controller}/{action}/{id}", // 带有参数的 URL
- new { controller ="Test", action = "TestHtmlView", id =UrlParameter.Optional } // 参数默认值
- );
- }
- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
- RegisterGlobalFilters(GlobalFilters.Filters);
- RegisterRoutes(RouteTable.Routes);
- }
- }
- }
- </span>
)
Step7:让我们开始执行程序,观察执行结果,如下图1:
图1
当我点击图1中“Html PsBk Click”按钮时,TestHtmlRedirect()方法被调用,并且视图重定向到TestAjaxView.cshtml,如下图:
图2
现在,考虑一件事,当我点击图2中"Ajax PsBk Click"按钮时,是否会发生同样的事,视图会重定向到About.cshtml?点击后,发现这件事并没有发生。
点击按钮后,TestAjaxRedirect()方法被调用,重定向语句段执行,但是视图并没有重定向。原因是:表单的提交使用了JavaScript的异步提交。正如我们看到的,操作的执行是异步的,Ajaxforms是适用于多种情况的,比如你需要修改或保存时是异步操作,但是不能重定向到其他表单。
二,下面,我们再做一个Demo,让我们测试一下Htmlforms和Ajax forms在执行修改操作时会有何不同。
Step8:定义一个实体 PersonnelModel
- <span style="font-family:KaiTi_GB2312;font-size:18px;">using System;
- usingSystem.Collections.Generic;
- using System.Linq;
- using System.Text;
- usingSystem.Reflection;
- using Model.Adapter;
- namespaceModel.Entity
- {
- public class PersonnelModel
- {
- public string UserName { get; set; }
- public string MailAdress { get; set; }
- }
- }</span>
Step9:再分别定义Html和Ajax视图
HtmlViewModel.cshtml:
- <span style="font-family:KaiTi_GB2312;font-size:18px;">@modelHtmlVsAjaxBeginForm.Models.PersonnelModel
- @{
- ViewBag.Title ="HtmlViewModel";
- }
- <h2>HtmlViewModel</h2>
- @using (Html.BeginForm("HtmlViewModel","Home",null))
- {
- @Html.ValidationSummary(true)
- <fieldset>
- <legend>PersonnelModel</legend>
- <divclassdivclass="editor-label">
- @Html.LabelFor(model =>model.UserName)
- </div>
- <divclassdivclass="editor-field">
- @Html.EditorFor(model =>model.UserName)
- @Html.ValidationMessageFor(model => model.UserName)
- </div>
- <divclassdivclass="editor-label">
- @Html.LabelFor(model =>model.MailAdress)
- </div>
- <divclassdivclass="editor-field">
- @Html.EditorFor(model =>model.MailAdress)
- @Html.ValidationMessageFor(model => model.MailAdress)
- </div>
- </fieldset>
- <p>
- <input type="submit"value="Html Form Action" />
- </p>
- }</span>
AjaxViewModel.cshtml:
- <span style="font-family:KaiTi_GB2312;font-size:18px;">@model Model.Entity.PersonnelModel
- @{
- ViewBag.Title = "AjaxViewModel";
- }
- <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
- <h2>AjaxViewModel</h2>
- @using (Ajax.BeginForm("AjaxViewModel", "Test", new AjaxOptions { UpdateTargetId = "result" }))
- {
- @Html.ValidationSummary(true)
- <fieldset>
- <legend>PersonnelModel</legend>
- <div id="result"></div>
- <div class="editor-label">
- @Html.LabelFor(model => model.UserName)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.UserName)
- @Html.ValidationMessageFor(model => model.UserName)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.MailAdress)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.MailAdress)
- @Html.ValidationMessageFor(model => model.MailAdress)
- </div>
- </fieldset>
- <p>
- <input type="submit" value="Ajax Form Action" />
- </p>
- }</span>
Step10:定义两个action方法,目的均为返回数据内容,显示在各自视图中
- <span style="font-family:KaiTi_GB2312;font-size:18px;">//HTML Form Method
- //Purpose: Will return the belowcontent, once after the method triggered.
- [HttpPost]
- public ActionResultHtmlViewModel(PersonnelModel Pmodel)
- {
- return Content("Hi" + Pmodel.UserName + ", Thanks for the details, a mail will be sentto " + Pmodel.MailAdress + " with all the login details.","text/html");
- }
- //AJAX Form Method
- //Purpose: Will return the belowcontent, once after the method triggered.
- [HttpPost]
- public ActionResultAjaxViewModel(PersonnelModel Pmodel)
- {
- return Content("Hi" + Pmodel.UserName + ", Thanks for the details, a mail will be sentto " + Pmodel.MailAdress + " with all the login details.","text/html");
- }</span>
Step11:现在分别运行这两个视图,点击各自按钮,观察执行效果:
HtmlViewModel.cshtml加载:
文本框中输入数据:
点击“Html Form Action”按钮后,运行效果:
弹出了新页面,将返回的数据显示
AjaxViewModel.cshtml加载:
文本框中输入数据:
点击“Ajax Form Action”按钮后,运行效果:
页面无刷新,将返回的数据显示在原页面
(注:当然在Html forms中也可以产生如上Ajaxfroms中的效果,例如:写js代码,使用Ajax请求函数)
总结:
Html.BeginForm与Ajax.BeginForm都是MVC架构中的表单元素;
区别:Html.BeginForm是普通的表单提交,而Ajax.BeginForm是支持异步的表单提交;
Ajax.BeginForm()优点:不用再自己去用JQ代码了,直接用MVC自代的Ajax.BeginForm就可以很容易的完成一个异步的表单提交动作。
Html.BeginForm() vs Ajax.BeginForm() in MVC3的更多相关文章
- 爱上MVC3系列~Html.BeginForm与Ajax.BeginForm
Html.BeginForm与Ajax.BeginForm都是MVC架构中的表单元素,它们从字面上可以看到区别,即Html.BeginForm是普通的表单提交,而Ajax.BeginForm是支持异步 ...
- 转:MVC3系列:~Html.BeginForm与Ajax.BeginForm
Html.BeginForm与Ajax.BeginForm都是MVC架构中的表单元素,它们从字面上可以看到区别,即Html.BeginForm是普通的表单提交,而Ajax.BeginForm是支持异步 ...
- MVC3系列~Html.BeginForm与Ajax.BeginForm
Html.BeginForm与Ajax.BeginForm都是MVC架构中的表单元素,它们从字面上可以看到区别,即Html.BeginForm是普通的表单提交,而Ajax.BeginForm是支持异步 ...
- Html.BeginForm())与Ajax.BeginForm()
一.@using (Html.BeginForm())参数示例 1.指定表单提交方式和路径等 @using (Html.BeginForm("Index", "Home ...
- MVC小系列(十一)【Html.BeginForm与Ajax.BeginForm】
Html.BeginForm与Ajax.BeginForm都是mvc的表单元素,前者是普通的表单提交,而后者是支持异步的表单提交,直接用mvc自带的Ajax.BeginForm就可以很容易完成一个异步 ...
- MVC Html.BeginForm 与 Ajax.BeginForm 使用总结
最近采用一边工作一边学习的方式使用MVC5+EF6做一个Demo项目, 期间遇到不少问题, 一直处于研究状态, 没能来得及记录. 今天项目进度告一段落, 得以有空记录学习中遇到的一些问题. 由于MVC ...
- form表单和ajax表单提交(Html.BeginForm()、Ajax.BeginForm())的差别
有如下几种区别: 1. Ajax在提交.请求.接收时,都是异步进行的,网页不需要刷新: Form提交则是新建一个页面,哪怕是提交给自己本身的页面,也是需要刷新的: 2. A在提交时,是在后台新建一个请 ...
- @using (Html.BeginForm()) @using (Ajax.BeginForm(new AjaxOptions() { })) 区别
@using (Html.BeginForm()) 返回页面 也是页面 都是返回页面 只是 多了一个 data-ajax="true"
- 【转】利用Ajax.BeginForm提交文件
Ajax.BeginForm @using (Ajax.BeginForm("YourAction", "YourController", new AjaxOp ...
随机推荐
- How to omit h1 title heading in HTML export
How to omit h1 title heading in HTML export */--> Introduce how to omit h1 title in the exported ...
- Linux基础入门学习笔记之二
第三节 用户及文件权限管理 Linux用户管理 Linux是可以实现多用户登录的操作系统 查看用户 who命令用于查看用户 shiyanlou是当前登录用户的用户名 pts/0中pts表示伪终端,后面 ...
- c++实现二叉树的非递归创建以及非递归先序、中序、后序遍历
二叉树的创建 思路:数组中从上到下依次放着二叉树中的元素,使用递归很容易实现,那么这里使用容器来存放之前的状态实现循环创建二叉树. TreeNode* createTree(int *arr, int ...
- 2017 Tag Cloud
距离上一篇随笔已经过去了三年多,惊讶地发现我还有个博客在这里 :) 越来越懒,这三年多就用下面这个tag cloud来总结好了
- Asp.net MVC4 +EF6开发的个人网站源码和介绍(仅供新手学习)
本项目是我去年利用业余时间开发的,采用的是asp.net mvc 4 +EF6+三层架构,适合新手进行学习,高手就没有什么价值了,可以直接跳过. 源码和数据库下载(已上传到git):https://g ...
- 【LOJ】#2037. 「SHOI2015」脑洞治疗仪
题解 维护区间内1的个数,左边数0的长度,右边数0的长度,区间内0区间最长个数,覆盖标记 第一种操作区间覆盖0 第二种操作查询\([l_0,r_0]\)中1的个数,区间覆盖0,然后覆盖时找到相对应的区 ...
- 操作系统基础梳理--进程&线程
1.举个栗子 1.1.知乎一形象例子 [摘自知乎]比如说十几年前,马化腾在写QQ,假设他那个时候不懂多进程多线程.然后他就开始写啦,这玩意儿不简单吗,不就是用户输入什么,把信息打包发到另一个用户,再显 ...
- chrome安装(sentos7)
在服务器上安装chrome是用来模拟浏览器抓取数据的. 直接 yum install chrome是安装不了的 你要做以下几步就可以了. 配置yum源 1. vim /etc/yum.repos.d/ ...
- iview-cli 项目、iView admin 跨域问题解决方案
在build 目录的 webpack.dev.config.js 目录中 module.exports = merge(webpackBaseConfig, { devtool: '#source-m ...
- 手动制作用于启动BeagleBoneBlack(am335x)的SD
1.需求MLO.u-boot.img.uImage.Systemfile 获取MLO.u-boot.img下载U-boot源码,解压获得源码,进入源码目录 cd /home/zyr/Source_co ...