1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc.Html {
\r
14 using System.Collections.Generic;
\r
15 using System.Web.Routing;
\r
17 public static class FormExtensions {
\r
18 public static MvcForm BeginForm(this HtmlHelper htmlHelper) {
\r
19 // generates <form action="{current url}" method="post">...</form>
\r
20 string formAction = htmlHelper.ViewContext.HttpContext.Request.RawUrl;
\r
21 return FormHelper(htmlHelper, formAction, FormMethod.Post, new RouteValueDictionary());
\r
24 public static MvcForm BeginForm(this HtmlHelper htmlHelper, object routeValues) {
\r
25 return BeginForm(htmlHelper, null, null, new RouteValueDictionary(routeValues), FormMethod.Post, new RouteValueDictionary());
\r
28 public static MvcForm BeginForm(this HtmlHelper htmlHelper, RouteValueDictionary routeValues) {
\r
29 return BeginForm(htmlHelper, null, null, routeValues, FormMethod.Post, new RouteValueDictionary());
\r
32 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName) {
\r
33 return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(), FormMethod.Post, new RouteValueDictionary());
\r
36 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues) {
\r
37 return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(routeValues), FormMethod.Post, new RouteValueDictionary());
\r
40 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues) {
\r
41 return BeginForm(htmlHelper, actionName, controllerName, routeValues, FormMethod.Post, new RouteValueDictionary());
\r
44 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method) {
\r
45 return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(), method, new RouteValueDictionary());
\r
48 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method) {
\r
49 return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(routeValues), method, new RouteValueDictionary());
\r
52 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method) {
\r
53 return BeginForm(htmlHelper, actionName, controllerName, routeValues, method, new RouteValueDictionary());
\r
56 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttributes) {
\r
57 return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(), method, new RouteValueDictionary(htmlAttributes));
\r
60 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, IDictionary<string, object> htmlAttributes) {
\r
61 return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(), method, htmlAttributes);
\r
64 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method, object htmlAttributes) {
\r
65 return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(routeValues), method, new RouteValueDictionary(htmlAttributes));
\r
68 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method, IDictionary<string, object> htmlAttributes) {
\r
69 string formAction = UrlHelper.GenerateUrl(null /* routeName */, actionName, controllerName, routeValues, htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true /* includeImplicitMvcValues */);
\r
70 return FormHelper(htmlHelper, formAction, method, htmlAttributes);
\r
73 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, object routeValues) {
\r
74 return BeginRouteForm(htmlHelper, null /* routeName */, new RouteValueDictionary(routeValues), FormMethod.Post, new RouteValueDictionary());
\r
77 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, RouteValueDictionary routeValues) {
\r
78 return BeginRouteForm(htmlHelper, null /* routeName */, routeValues, FormMethod.Post, new RouteValueDictionary());
\r
81 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName) {
\r
82 return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(), FormMethod.Post, new RouteValueDictionary());
\r
85 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, object routeValues) {
\r
86 return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(routeValues), FormMethod.Post, new RouteValueDictionary());
\r
89 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, RouteValueDictionary routeValues) {
\r
90 return BeginRouteForm(htmlHelper, routeName, routeValues, FormMethod.Post, new RouteValueDictionary());
\r
93 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, FormMethod method) {
\r
94 return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(), method, new RouteValueDictionary());
\r
97 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, object routeValues, FormMethod method) {
\r
98 return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(routeValues), method, new RouteValueDictionary());
\r
101 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, RouteValueDictionary routeValues, FormMethod method) {
\r
102 return BeginRouteForm(htmlHelper, routeName, routeValues, method, new RouteValueDictionary());
\r
105 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, FormMethod method, object htmlAttributes) {
\r
106 return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(), method, new RouteValueDictionary(htmlAttributes));
\r
109 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, FormMethod method, IDictionary<string, object> htmlAttributes) {
\r
110 return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(), method, htmlAttributes);
\r
113 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, object routeValues, FormMethod method, object htmlAttributes) {
\r
114 return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(routeValues), method, new RouteValueDictionary(htmlAttributes));
\r
117 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, RouteValueDictionary routeValues, FormMethod method, IDictionary<string, object> htmlAttributes) {
\r
118 string formAction = UrlHelper.GenerateUrl(routeName, null, null, routeValues, htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, false /* includeImplicitMvcValues */);
\r
119 return FormHelper(htmlHelper, formAction, method, htmlAttributes);
\r
122 public static void EndForm(this HtmlHelper htmlHelper) {
\r
123 HttpResponseBase httpResponse = htmlHelper.ViewContext.HttpContext.Response;
\r
124 httpResponse.Write("</form>");
\r
127 private static MvcForm FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> htmlAttributes) {
\r
128 TagBuilder tagBuilder = new TagBuilder("form");
\r
129 tagBuilder.MergeAttributes(htmlAttributes);
\r
130 // action is implicitly generated, so htmlAttributes take precedence.
\r
131 tagBuilder.MergeAttribute("action", formAction);
\r
132 // method is an explicit parameter, so it takes precedence over the htmlAttributes.
\r
133 tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true);
\r
135 HttpResponseBase httpResponse = htmlHelper.ViewContext.HttpContext.Response;
\r
136 httpResponse.Write(tagBuilder.ToString(TagRenderMode.StartTag));
\r
137 return new MvcForm(htmlHelper.ViewContext.HttpContext.Response);
\r