开发MVC+jQuery下B/S系统的表单提交

开发 后端
这里将介绍如何用MVC+jQuery来开发B/S系统中的提交表单功能,需要引入一些插件,希望本文能对大家有所帮助。

我们这里将说到如果在ajax提交表单,但这里将介绍MVC+jQuery的处理方法。需要实现的是1、jquery.form.js的使用、2、lightbox的实现、3、如何验证。

说起表单提交,是无人不知,无人不晓哇。今天我们就谈如何用MVC+jQuery来处理表单的提交。

想达到的效果:

1、提交前的表单验证

2、表单验证

3、提交后的反馈信息

ok,先说一下工作的原理。

前台 ,action指定了接受表单的处理页面。method这里我们只说post

如果用ajax提交表单,自然xxx.aspx便是请求的url。ajax请求后的callback便是响应表单的提交结果(错误、成功),我们提交的反馈信息用一个 浮层(lightbox)来表示。 不至于用 alert(""); 这样铛铛铛很囧。

我们引入一个jQuery的插件http://www.malsup.com/jquery/form/#api 这是一个略有名气的插件,方便易用。

关于其用法,大家可以搜索下。

那么我们需要做的就是

1、jquery.form.js的使用

2、lightbox的实现

3、如何验证

Code

  1. //注册form的ajaxForm 此方法需要调用jquery.ajaxwindow.js的方法  
  2. //一般form里有action,所以参数有可能只需要传一个callback,  
  3. //如果一个表单有多个提交按钮,那么则需要 提交不同的url  
  4. // 这个方法是用来注册form提交,如果有多个提交按钮时,最好默认注册一个,否则验证方法就不起到作用  
  5. $.fn.submitForm = function(args) {  
  6.     var url, id, callback, before;  
  7.     id = this.attr("id");  
  8.  
  9.     if (typeof (args) == "string") {//只传一个url  
  10.         url = args;  
  11.         before = undefined;  
  12.         callback = undefined;  
  13.     }  
  14.     else {  
  15.         args = args || new Object();  
  16.         url = args.url || this.attr("action");  
  17.         if (typeof (args) == "function") {//只传一个callback  
  18.             callback = args;  
  19.         }  
  20.         else {  
  21.             before = args.before;  
  22.             callback = args.callback;  
  23.         }  
  24.     }  
  25.     //输入验证  
  26.     this.inputValidate();//这是我们需要实现的一个“输入时的验证”,  
  27.     this.ajaxForm({ //这里调用jquery.form.js表单注册方法。  
  28.         url: url,  
  29.         beforeSubmit: function(a, f, o) {//提交前的处理  
  30.             //提交验证  
  31.             if ($("#" + id).submitValidate() == false)//这里我们需要实现的“提交时的验证”。  
  32.                 return false;  
  33.             if (before != undefined && before() == false) {  
  34.                 return false;  
  35.             }  
  36.             o.dataType = "json";//指定返回的数据为json格式。  
  37.         },  
  38.  
  39.         success: function(data) {//提交后反馈信息处理  
  40.             if (data == "" || data == null) {  
  41.                 return false;  
  42.             }  
  43.             var msg = new ajaxMsg(data);//这个ajaxMsg便是我们需要实现的Lightbox  
  44.             msg.show(callback);//show这个反馈的结果。  
  45.             return false;  
  46.         }  
  47.     });  
  48.     return this;  

上面的方法很简单,就是form插件的使用,还有几个我们需要实现的方法。(inputValidate,submitValiedate,ajaxMsg)
既然是ajax提交,我们则需要给客户端一个反馈信息,然后用Lightbox呈现。

一:我们定义一个JsonMessage类

  1. Code  
  2.     public class JsonMessage  
  3.     {  
  4.         public int result { getset; } //0错误 1正确 2提示 3警告  
  5.         public string title { getset; }//Lightbox窗口的标题  
  6.         public string content { getset; }//message的具体内容  
  7.         public string redirect { getset; }//弹出后自动跳转的到哪里?  
  8.         public object callBackData { getset; }//客户端需要的数据 比如 一个新增的id或整个model 

MVC返回Json(jsonmsg1),客户端的callback便可以得到这个对象的json格式。

(注意:如果是附件的表单提交,则不能识别JsonResult格式。具体我还没有研究怎么回事,这种情况只能response一个json字符串,可以用System.Web.Script.Serialization.JavaScriptSerializer来序列化对象,也很方便。)

二:我们写一个ajaxMsg来实现lightbox,这是我自己写的Lightbox,比较简陋,如果不喜欢,也可以用一些知名的插件。

  1. Code  
  2. var ajaxMsg = function(msg) {  
  3.     if (msg != undefined) {  
  4.         //提示框的属性  
  5.         this.result = msg.result || 0; //0错误 1正确 2提示 3警告  
  6.         this.title = msg.title || ""//提示框的标题  
  7.         this.redirect = msg.redirect || null;  
  8.         this.content = msg.content || ""//窗口的内容 通过后台json数据获得  
  9.         this.callBackData = msg.callBackData;  
  10.     }  
  11. }  
  12. //创建一个win  
  13. ajaxMsg.prototype.open = function(parentElement) {  
  14.     //创建Mask遮罩层  
  15.     $("body").append("  
  16.  
  17. ");  
  18.     //设置Mask高度  
  19.     var bodyheight = document.body.offsetHeight;  
  20.     var clientheight = document.documentElement.clientHeight;  
  21.     if (bodyheight > clientheight)  
  22.         clientheight = bodyheight;  
  23.     else 
  24.         bodyheight = clientheight;  
  25.     $("#MsgMask").height(bodyheight + "px");  
  26.  
  27.     //创建窗口  
  28.     $("body").append("  
  29.  
  30. ");  
  31.     var frameId = "#MsgFrame";  
  32.  
  33.     //不同的style 不同的frame  frame由mask来控制  
  34.     switch (this.result) {  
  35.         case 0:  
  36.             $(frameId).addClass("msg_error");  
  37.             break;  
  38.         case 1:  
  39.             $(frameId).addClass("msg_right");  
  40.             break;  
  41.         case 2:  
  42.             $(frameId).addClass("msg_tips");  
  43.             break;  
  44.         case 3:  
  45.             $(frameId).addClass("msg_warning");  
  46.             break;  
  47.         default:  
  48.             $(frameId).addClass("msg_default");  
  49.             break;  
  50.     }  
  51.     //创建内容div  
  52.     $(frameId).append("  
  53.  
  54. " + this.content + " 
  55. ");  
  56.  
  57.     //设置Mask的宽高  
  58.     if (this.width > 0)  
  59.         $(frameId).width(this.width);  
  60.     if (this.height > 0)  
  61.         $(frameId).height(this.height);  
  62.  
  63.     //设置窗口的定位  
  64.     var frame_width = $(frameId).width();  
  65.     var frame_height = $(frameId).height();  
  66.     var css_top = parseInt((document.documentElement.clientHeight - frame_height) / 2) - 100;  
  67.     if (css_top <= 0)  
  68.         css_top = 80;  
  69.     var css_left = (document.documentElement.clientWidth - frame_width) / 2;  
  70.     var css_right = css_left;  
  71.     $(frameId).css("top", css_top + "px").css("left", css_left + "px").css("right", css_right + "px");  
  72.     hideSelects();  
  73. }  
  74.  
  75. //显示方式  
  76. ajaxMsg.prototype.show = function(callBack) {  
  77.     if (this.result == undefined) {  
  78.         if (callBack != undefined)  
  79.             callBack(this.callBackData);  
  80.         return;  
  81.     }  
  82.     if (this.result == 1) {  
  83.         if (this.content != undefined && this.content != "") {  
  84.             //this.open();  
  85.             //setTimeout(this.close, 1000);  
  86.             alert(this.content);  
  87.         }  
  88.         if (callBack != undefined)  
  89.             callBack(this.callBackData);  
  90.     }  
  91.     else {  
  92.         //this.open();  
  93.         //setTimeout(this.close, 2000);  
  94.         alert(this.content);  
  95.     }  
  96. }  
  97.  
  98. //关闭  
  99. ajaxMsg.prototype.close = function() {  
  100.     removeMsg();  
  101. }  
  102. function removeMsg() {  
  103.     $("div").remove("#MsgMask");  
  104.     $("div").remove("#MsgFrame");  
  105.     showSelects();  

不知道有没有人能理解我这里的callback,我说一下他的用到的情况。 实际应用中我还有一个ajaxWin来实现弹窗,弹窗里的表单提交后一般需要关闭弹窗,然后更新一些数据(比如刷新列表)。这时就需要 submit后的callback动作。另外就是我目前还有使用到redirect这个属性。呵呵,我之前的系统需要大量的跳转处理,这些跳转也是无刷新的,有一个数组来记录访问的地址。可以实现后退和前进。

下面是他的css

  1. Code  
  2. .mask  
  3. {  
  4.     background-color: #fff;  
  5.     width: 100%;  
  6.     height: 100%;  
  7.     position: absolute;  
  8.     display: block;  
  9.     top: 0px;  
  10.     left: 0px;  
  11.     filter:alpha(opacity=0);-moz-opcacity:0;opacity:0;  
  12.     z-index:1;  
  13. }  
  14.  
  15. .frame  
  16. {  
  17.     position: absolute;  
  18.     z-index: 2;  
  19.     min-width:400px;  
  20.     font-size:12px;  
  21.     background-color:#fff;  
  22.     overflow:hidden  
  23. }  
  24. .ajaxwin  
  25. {  
  26.     border: 1px solid #ccc;  
  27.     text-align:left;  
  28. }  
  29. .frame .head  
  30. {  
  31.     line-height:25px;  
  32.     background-color: #d3eaff;  
  33.     clear:both;  
  34.       
  35. }  
  36. .frame .head .title  
  37. {  
  38.     position:relative;  
  39.     left:5px;  
  40.     font-size: 12px;  
  41.     font-weight: bold;  
  42.     color: #000;  
  43. }  
  44. .frame .head .btnclose  
  45. {  
  46.     position:absolute;  
  47.     top:0px;  
  48.     right:5px;  
  49. }  
  50. .default 
  51. {  
  52.     border: 1px solid #95A1B6;  
  53. }  
  54. .msg_error  
  55. {  
  56.     border: 2px solid #FF6600;  
  57.     background-color: #FFFFCC;  
  58.     font-size: 14px;  
  59.     overflow: hidden;  
  60.     font-weight: bold;  
  61. }  
  62. .msg_error .head,.msg_tips .head,.msg_right .head{display:none;}  
  63. .msg_tips,.msg_default  
  64. {  
  65.     border: 2px solid #3399FF;  
  66.     background-color: #E6FFFF;  
  67.     font-size: 14px;  
  68.     height: 60px;  
  69.     overflow: hidden;  
  70.     font-weight: bold;  
  71. }  
  72. .msg_right  
  73. {  
  74.     border: 2px solid #009933;  
  75.     background-color: #E8FFD0;  
  76.     font-size: 14px;  
  77.     height: 60px;  
  78.     overflow: hidden;  
  79.     font-weight: bold;  
  80. }  
  81.  
  82. .content{ padding:25px; text-align:center;}  
  83. .default .content,.ajaxwin .content{ padding:10px; text-align:left; }  
  84.  
  85. .frame .btnclose  
  86. {  
  87.     padding:5px; 

三:好了,Lightbox已经实现了,也能show出各种类型的信息了。

下面还剩下表单验证。 其实表单验证大有文章可做。我这也不能一一做到。目前只做了些简单的验证。以后会实现比较复杂的错误提示效果。其实这都是 体力活,上面没要求我也懒的弄-。-

验证我采用的是给control一些自定义属性,然后再判断其值是否合法。

  1. Code  
  2. //输入验证  
  3. $.fn.inputValidate = function() {  
  4.     $("input,select,textarea"this).each(function() {  
  5.         var isnull = $(this).attr("isnull");  
  6.         var regexValue = $(this).attr("regex");  
  7.         var defautValue = $(this).attr("dvalue");  
  8.  
  9.         //            //①非空注册焦点事件  
  10.         //            if (isnull == "0") {  
  11.         //                $(this).blur(function() {  
  12.         //                    if (this.value == "" || this.value == defautValue)  
  13.         //                        $(this).addClass("focus").focus();  
  14.         //                    else  
  15.         //                        $(this).removeClass("focus");  
  16.         //                });  
  17.         //            }  
  18.  
  19.         //②正则注册onchange事件  
  20.         if (regexValue != undefined) {  
  21.             var thisValue = this.value;  
  22.             //检查类型绑定不同事件  
  23.             if ($(this).attr("type") == "text") {  
  24.                 $(this).bind("keyup", function() {  
  25.                     if ($(this).val() == "")  
  26.                         return;  
  27.                     var re = new RegExp(regexValue, "ig");  
  28.                     var newValue = this.value;  
  29.                     if (!re.test(newValue)) {  
  30.                         $(this).val(thisValue).focus();  
  31.                     }  
  32.                     else {  
  33.                         thisValue = newValue;  
  34.                         $(this).val(newValue);  
  35.                     }  
  36.                 });  
  37.             }  
  38.         }  
  39.  
  40.         function checkRegex(value, re) {  
  41.  
  42.         }  
  43.         //③最小长度  
  44.  
  45.         //④其他  
  46.  
  47.     });  
  48. }  
  49.  
  50. //提交验证  
  51. $.fn.submitValidate = function() {  
  52.     var result = true;  
  53.     $("input:visible,select:visible,textarea:visible"this).each(function() {  
  54.         result = true;  
  55.         var thisValue = "";  
  56.         if ($(this).attr("type") == "radio" || $(this).attr("type") == "checkbox") {  
  57.             thisValue = $("input[name='" + this.name + "']:checked").val();  
  58.         }  
  59.         else 
  60.             thisValue = $(this).val();  
  61.         //判断是否违法  
  62.  
  63.  
  64.         if ($(this).attr("isnull") == "0") {//① 是否必填 且不能为空或缺省值  
  65.             result = (thisValue != "" && thisValue != $(this).attr("dvalue"));  
  66.         }  
  67.         else if (thisValue != "") {//② 是否符合格式 属性为 regex 正则  
  68.             var reValue = $(this).attr("regex");  
  69.             if (reValue != undefined) {  
  70.                 re = new RegExp(reValue, "ig");  
  71.                 result = re.test(thisValue);  
  72.             }  
  73.         }  
  74.  
  75.         //        //③ 是否符合最大长度  
  76.         //        var maxLength = $(this).attr("maxLen");  
  77.         //        if (maxLength != undefined && maxLength != "-1") {  
  78.         //            if (thisValue.length > parseInt(maxLength))  
  79.         //                result = false;  
  80.         //        }  
  81.         //        //④ 是否符合最小长度  
  82.  
  83.         //返回false  
  84.  
  85.         if (result == false) {  
  86.             $(this).addClass("focus").focus().blur(function() {  
  87.                 if (this.value != "" && this.value != $(this).attr("dvalue")) {  
  88.                     $(this).removeClass("focus");  
  89.                 }  
  90.             });  
  91.             //alert($(this).attr("name"));  
  92.             return false;  
  93.         }  
  94.     });  
  95.     return result;  

原文标题:MVC+Jquery开发B/S系统:③表单提交

链接:http://www.cnblogs.com/mad/archive/2009/09/14/1566231.html

【编辑推荐】

  1. jQuery调用WCF服务传递JSON对象
  2. 学习jQuery必须知道的几种常用方法
  3. 用XML+XSLT+CSS+JQuery组建ASP.NET网站
  4. 使用jQuery和PHP构建一个受Ajax驱动的Web页面
  5. jQuery调用WCF需要注意的一些问题

【责任编辑:彭凡 TEL:(010)68476606】

责任编辑:彭凡 来源: 博客园
相关推荐

2011-04-19 10:32:27

MVCjQuery

2009-05-18 10:11:06

MVCXML动态表单

2012-06-05 10:15:43

jQuery

2009-07-29 16:40:50

Ajax提交asp.n

2009-03-31 13:12:05

ASP.NETMVC表单验证

2010-11-23 16:56:04

mysql表单

2012-12-24 10:00:07

ASP.NETjQueryAjax

2013-11-13 11:01:14

表单表单重复提交表单策略

2009-06-25 14:32:00

Java BS开发模式

2013-11-13 14:39:53

表单提交开发

2009-12-01 18:11:03

PHP表单重复提交

2010-07-29 16:38:14

Flex表单

2023-04-11 07:50:27

软件架构设计

2011-06-16 10:21:52

jQuery

2010-03-04 11:36:02

Python提交表单

2012-03-29 09:27:49

WEBjQuery

2011-06-28 09:24:44

jQuery

2012-03-08 11:23:09

jQuery Mobi

2009-06-30 15:19:55

Form表单JSP入门

2012-03-27 14:34:07

Visual Stud微软MVC
点赞
收藏

51CTO技术栈公众号