表单提交的方式

1.传统Form表单Aciton属性提交.
点击submit按钮后,将表单提交到form的action属性指定的路径
2.Jquery+Ajax 提交表单
  1. <form action="" id="form_demo" method="post">  
  2.   <input type="text" name="a" value="1" id="a" /> 
  3.  <input type="text" name="b" value="2" id="a" /> 
  4.   <input type="submit" name="g" value="Submit" id="g" />   
  5. </form>  
  1.   $('#form_demo').submit(function () {  
  2.   
  3.             $.ajax({  
  4.                 cache: true,  
  5.                 type: "POST",  
  6.                 url: "test.ashx",  
  7.                 data: $('#form_demo').serialize(),  
  8.                 async: false,  
  9.                 error: function (request) {  
  10.                     alert("Connection error");  
  11.                 },  
  12.                 success: function (data) {  
  13.                     alert("Connection Success->" + data);  
  14.                 }  
  15.             });  
  16.   
  17.             return false;  
  18.         });  
  • form_demo为form元素的id属性值
  • serialize()》》通过序列化表单值,创建 URL 编码文本字符串。在这个例子中结果是:a=1&b=2。
  • 注意;只会将”成功的控件“序列化为字符串。如果不使用按钮来提交表单,则不对提交按钮的值序列化。如果要表单元素的值包含到序列字符串中,元素必须使用 name 属性。
 
3.@using (Html.BeginForm((this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method, object htmlAttributes))。)    
  • 使用Html.BeginForm(方法会生成一个form表单,需要用using把该方法括起来,使系统知道form表单从何处开始,何处结束。
  • 参数:
actionName类型:System.String。操作方法的名称;
controllerName:类型:System.String。控制器的名称;
routeValues:一个包含路由参数的对象。通过检查对象的属性,利用反射检索参数。此对象通常是使用对象初始值设定项语法创建的
method类型:System.Web.Mvc.FormMethod。用于处理窗体的 HTTP 方法(GET 或 POST)。
htmlAttributes类型:System.Object一个对象,其中包含要为该元素设置的 HTML 特性;
  • 实例:@using (Html.BeginForm("UserVisitList", "User",  new { UserID = "200" },FormMethod.Post,new{id=pic})){}      
  • 生成的form表单:<form action="/jbUser/User/UserVisitList?UserID=200 " id="pic" method="post"></form>(其中jbUser指的是area的名称)
  • 注意:Html.BeginForm()中参数的顺序很重要,
 
另外:路由参数除了可以用new { UserID = "200" }的形式外,还可以通过表单元素name属性值与表单提交的动作参数名字一模一样来传值。如:
  @using (Html.BeginForm("UserVisitList", "User", FormMethod.Post, new { id = "pic" }))
                {
                    <input id="UserID" name="UserID" type="hidden" value="3"/>
                    <input id="TypeID" name="TypeID" type="hidden" value="4" />
<textarea id="txtContent" rows="3" cols="100" name="txtContent" style="width: 680px; height: 350px;">.txtContent</textarea>
t
}//可以看到这时我没有在Html.BeginForm()中设置路由参数。另外,这种情况更方便。
 
表单提交的动作:public ActionResult UserVisitList(string UserID, string TypeID,string txtContent)()
 
当提交表单调用UserVisitList()动作时,参数UserID的值就是name=“UserID”的input元素的值,参数TypeID就是name=“TypeID”的input元素的值,txtContent同理。
 
但是:既在Html.BeginForm()中设置路由参数,同时表单元素name属性与动作参数一模一样的情况呢?
这时后者优先。
比如:
@using (Html.BeginForm("UserVisitList", "User",  new { UserID = "200" },FormMethod.Post,new{id=pic})){
<input id="UserID" name="UserID" type="hidden" value="3"/>;
}  
这时UserVisitList(string UserID, string TypeID,string txtContent)()接收到的UserID值为“3”.
 
4.Ajax.BeginForm()
  • 在Asp.Net的MVC中的语法,在Razor页面中使用,替代JQuery的Ajax使用,方便快捷。
  • 使用Ajax.BeginForm方法会生成一个form表单,最后以Ajax的方式提交表单数据;需要用using把该方法括起来,使系统知道form表单从何处开始,何处结束。
5.BeginRouteForm 方法
 
posted @ 2017-07-10 09:49  coderHigh  阅读(397)  评论(0编辑  收藏  举报