1 namespace System.Web.Mvc.Html {
2     using System.Collections.Generic;
3     using System.Diagnostics.CodeAnalysis;
4     using System.Web.Routing;
5 
6     public static class FormExtensions {
BeginForm(this HtmlHelper htmlHelper)7         public static MvcForm BeginForm(this HtmlHelper htmlHelper) {
8             // generates <form action="{current url}" method="post">...</form>
9             string formAction = htmlHelper.ViewContext.HttpContext.Request.RawUrl;
10             return FormHelper(htmlHelper, formAction, FormMethod.Post, new RouteValueDictionary());
11         }
12 
BeginForm(this HtmlHelper htmlHelper, object routeValues)13         public static MvcForm BeginForm(this HtmlHelper htmlHelper, object routeValues) {
14             return BeginForm(htmlHelper, null, null, new RouteValueDictionary(routeValues), FormMethod.Post, new RouteValueDictionary());
15         }
16 
BeginForm(this HtmlHelper htmlHelper, RouteValueDictionary routeValues)17         public static MvcForm BeginForm(this HtmlHelper htmlHelper, RouteValueDictionary routeValues) {
18             return BeginForm(htmlHelper, null, null, routeValues, FormMethod.Post, new RouteValueDictionary());
19         }
20 
BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName)21         public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName) {
22             return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(), FormMethod.Post, new RouteValueDictionary());
23         }
24 
BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues)25         public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues) {
26             return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(routeValues), FormMethod.Post, new RouteValueDictionary());
27         }
28 
BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues)29         public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues) {
30             return BeginForm(htmlHelper, actionName, controllerName, routeValues, FormMethod.Post, new RouteValueDictionary());
31         }
32 
BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method)33         public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method) {
34             return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(), method, new RouteValueDictionary());
35         }
36 
BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method)37         public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method) {
38             return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(routeValues), method, new RouteValueDictionary());
39         }
40 
BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method)41         public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method) {
42             return BeginForm(htmlHelper, actionName, controllerName, routeValues, method, new RouteValueDictionary());
43         }
44 
BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttributes)45         public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttributes) {
46             return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(), method, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
47         }
48 
BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, IDictionary<string, object> htmlAttributes)49         public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, IDictionary<string, object> htmlAttributes) {
50             return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(), method, htmlAttributes);
51         }
52 
BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method, object htmlAttributes)53         public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method, object htmlAttributes) {
54             return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(routeValues), method, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
55         }
56 
BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method, IDictionary<string, object> htmlAttributes)57         public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method, IDictionary<string, object> htmlAttributes) {
58             string formAction = UrlHelper.GenerateUrl(null /* routeName */, actionName, controllerName, routeValues, htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true /* includeImplicitMvcValues */);
59             return FormHelper(htmlHelper, formAction, method, htmlAttributes);
60         }
61 
BeginRouteForm(this HtmlHelper htmlHelper, object routeValues)62         public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, object routeValues) {
63             return BeginRouteForm(htmlHelper, null /* routeName */, new RouteValueDictionary(routeValues), FormMethod.Post, new RouteValueDictionary());
64         }
65 
BeginRouteForm(this HtmlHelper htmlHelper, RouteValueDictionary routeValues)66         public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, RouteValueDictionary routeValues) {
67             return BeginRouteForm(htmlHelper, null /* routeName */, routeValues, FormMethod.Post, new RouteValueDictionary());
68         }
69 
BeginRouteForm(this HtmlHelper htmlHelper, string routeName)70         public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName) {
71             return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(), FormMethod.Post, new RouteValueDictionary());
72         }
73 
BeginRouteForm(this HtmlHelper htmlHelper, string routeName, object routeValues)74         public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, object routeValues) {
75             return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(routeValues), FormMethod.Post, new RouteValueDictionary());
76         }
77 
BeginRouteForm(this HtmlHelper htmlHelper, string routeName, RouteValueDictionary routeValues)78         public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, RouteValueDictionary routeValues) {
79             return BeginRouteForm(htmlHelper, routeName, routeValues, FormMethod.Post, new RouteValueDictionary());
80         }
81 
BeginRouteForm(this HtmlHelper htmlHelper, string routeName, FormMethod method)82         public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, FormMethod method) {
83             return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(), method, new RouteValueDictionary());
84         }
85 
BeginRouteForm(this HtmlHelper htmlHelper, string routeName, object routeValues, FormMethod method)86         public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, object routeValues, FormMethod method) {
87             return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(routeValues), method, new RouteValueDictionary());
88         }
89 
BeginRouteForm(this HtmlHelper htmlHelper, string routeName, RouteValueDictionary routeValues, FormMethod method)90         public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, RouteValueDictionary routeValues, FormMethod method) {
91             return BeginRouteForm(htmlHelper, routeName, routeValues, method, new RouteValueDictionary());
92         }
93 
BeginRouteForm(this HtmlHelper htmlHelper, string routeName, FormMethod method, object htmlAttributes)94         public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, FormMethod method, object htmlAttributes) {
95             return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(), method, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
96         }
97 
BeginRouteForm(this HtmlHelper htmlHelper, string routeName, FormMethod method, IDictionary<string, object> htmlAttributes)98         public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, FormMethod method, IDictionary<string, object> htmlAttributes) {
99             return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(), method, htmlAttributes);
100         }
101 
BeginRouteForm(this HtmlHelper htmlHelper, string routeName, object routeValues, FormMethod method, object htmlAttributes)102         public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, object routeValues, FormMethod method, object htmlAttributes) {
103             return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(routeValues), method, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
104         }
105 
BeginRouteForm(this HtmlHelper htmlHelper, string routeName, RouteValueDictionary routeValues, FormMethod method, IDictionary<string, object> htmlAttributes)106         public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, RouteValueDictionary routeValues, FormMethod method, IDictionary<string, object> htmlAttributes) {
107             string formAction = UrlHelper.GenerateUrl(routeName, null, null, routeValues, htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, false /* includeImplicitMvcValues */);
108             return FormHelper(htmlHelper, formAction, method, htmlAttributes);
109         }
110 
EndForm(this HtmlHelper htmlHelper)111         public static void EndForm(this HtmlHelper htmlHelper) {
112             htmlHelper.ViewContext.Writer.Write("</form>");
113             htmlHelper.ViewContext.OutputClientValidation();
114         }
115 
116         [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Because disposing the object would write to the response stream, you don't want to prematurely dispose of this object.")]
FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> htmlAttributes)117         private static MvcForm FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> htmlAttributes) {
118             TagBuilder tagBuilder = new TagBuilder("form");
119             tagBuilder.MergeAttributes(htmlAttributes);
120             // action is implicitly generated, so htmlAttributes take precedence.
121             tagBuilder.MergeAttribute("action", formAction);
122             // method is an explicit parameter, so it takes precedence over the htmlAttributes.
123             tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true);
124 
125             bool traditionalJavascriptEnabled = htmlHelper.ViewContext.ClientValidationEnabled
126                                             && !htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled;
127 
128             if (traditionalJavascriptEnabled) {
129                 // forms must have an ID for client validation
130                 tagBuilder.GenerateId(htmlHelper.ViewContext.FormIdGenerator());
131             }
132 
133             htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag));
134             MvcForm theForm = new MvcForm(htmlHelper.ViewContext);
135 
136             if (traditionalJavascriptEnabled) {
137                 htmlHelper.ViewContext.FormContext.FormId = tagBuilder.Attributes["id"];
138             }
139 
140             return theForm;
141         }
142     }
143 }
144