ASP.NET MVC-- 异步表单(AJAX的另一种方法)
在ASP.NET MVC中,form这个好东西重新热了起来.现在一个页面中可以有多个form了.今天在QQ群上有位兄弟问到如何在一个Action的View上向另一个Action Post表单并获取一个执行的结果,其实这个自然可以不用form直接ajax另一个Action就OK了,但是form自然有form的好处(好处就不必说了,自己体验吧),而且ajax的话你必然要写很多的js代码. 于是我充分发挥高科技的Google搜索引擎,搜索"JS FORM提交并获取返回值" ,搜到了一些把js快用到不是人的境界的高手写的一些人看不懂的代码,另外还有一些风格很不好的(在页面中插iframe)实现.对这些我很~~不满意啊... 后来突然灵光一现Ajax.BeginForm就跳了出来.
Ajax.BeginForm大致如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, AjaxOptions ajaxOptions);
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// actionName:
// The name of the action.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, AjaxOptions ajaxOptions);
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// actionName:
// The name of the action.
//
// routeValues:
// An object containing the parameters for a route. The parameters are retrieved
// via reflection by examining the properties of the object. Typically created
// using object initializer syntax.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, object routeValues, AjaxOptions ajaxOptions);
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// actionName:
// The name of the action.
//
// routeValues:
// An object containing the parameters for a route.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions);
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// actionName:
// The name of the action.
//
// controllerName:
// The name of the controller.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, AjaxOptions ajaxOptions);
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// actionName:
// The name of the action.
//
// routeValues:
// An object containing the parameters for a route. The parameters are retrieved
// via reflection by examining the properties of the object. Typically created
// using object initializer syntax.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// htmlAttributes:
// An object containing the HTML attributes for the element. The parameters
// are retrieved via reflection by examining the properties of the object. Typically
// created using object initializer syntax.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes);
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// actionName:
// The name of the action.
//
// routeValues:
// An object containing the parameters for a route.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// htmlAttributes:
// An object containing the HTML attributes for the element.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes);
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// actionName:
// The name of the action.
//
// controllerName:
// The name of the controller.
//
// routeValues:
// An object containing the parameters for a route. The parameters are retrieved
// via reflection by examining the properties of the object. Typically created
// using object initializer syntax.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions);
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// actionName:
// The name of the action.
//
// controllerName:
// The name of the controller.
//
// routeValues:
// An object containing the parameters for a route.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions);
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// actionName:
// The name of the action.
//
// controllerName:
// The name of the controller.
//
// routeValues:
// An object containing the parameters for a route. The parameters are retrieved
// via reflection by examining the properties of the object. Typically created
// using object initializer syntax.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// htmlAttributes:
// An object containing the HTML attributes for the element. The parameters
// are retrieved via reflection by examining the properties of the object. Typically
// created using object initializer syntax.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes);
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// actionName:
// The name of the action.
//
// controllerName:
// The name of the controller.
//
// routeValues:
// An object containing the parameters for a route.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// htmlAttributes:
// An object containing the HTML attributes for the element.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes);
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// routeName:
// The name of the route to use to obtain the form post URL.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, AjaxOptions ajaxOptions);
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// routeName:
// The name of the route to use to obtain the form post URL.
//
// routeValues:
// An object containing the parameters for a route. The parameters are retrieved
// via reflection by examining the properties of the object. Typically created
// using object initializer syntax.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, object routeValues, AjaxOptions ajaxOptions);
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// routeName:
// The name of the route to use to obtain the form post URL.
//
// routeValues:
// An object containing the parameters for a route.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions);
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// routeName:
// The name of the route to use to obtain the form post URL.
//
// routeValues:
// An object containing the parameters for a route. The parameters are retrieved
// via reflection by examining the properties of the object. Typically created
// using object initializer syntax.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// htmlAttributes:
// An object containing the HTML attributes for the element. The parameters
// are retrieved via reflection by examining the properties of the object. Typically
// created using object initializer syntax.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes);
//
// 摘要:
// Writes an opening form tag to the response while returning an System.Web.Mvc.Html.MvcForm
// instance. Can be used in a using block, in which case it renders the closing
// form tag at the end of the using block. The form is submitted asynchronously
// using JavaScript.
//
// 参数:
// ajaxHelper:
// The ajax helper.
//
// routeName:
// The name of the route to use to obtain the form post URL.
//
// routeValues:
// An object containing the parameters for a route.
//
// ajaxOptions:
// An object providing options for the asynchronous request.
//
// htmlAttributes:
// An object containing the HTML attributes for the element.
//
// 返回结果:
// An System.Web.Mvc.Html.MvcForm instance.
public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes);
大致的参数含义如下:
actionName,controllerName:自然是Action和Controller的名字了.
AjaxOptions:这个就是让Form能够Ajax的关键部分了.它包含的成员的含义我也不再赘述,只要看得懂英文的人基本上都能明白那个意思.
routeValues:我的猜测是route中的参数值吧.(可惜不知道哪儿有ASP.NET MVC 的 API)
这样我如下实现就OK了.
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%using (Ajax.BeginForm("AddUser", new AjaxOptions { LoadingElementId = "Loading", UpdateTargetId = "AjaxResult", InsertionMode = InsertionMode.InsertBefore, OnSuccess = "ProcessSuccess" }))
{ %>
<%=Html.TextBox("UserName","a") %>
<%=Html.TextBox("PassWord","b") %>
<%=Html.TextBox("CheckPassWord","c") %>
<input type="submit" value="add" />
<%} %>
感觉微软好像没有公布MVC的API,很多东西都不太知道含义,尤其是现在匿名类用的比较多一堆的Object鬼知道什么含义,什么规格才能良好的工作.