Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / Html / FormExtensions.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\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
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc.Html {\r
14     using System.Collections.Generic;\r
15     using System.Web.Routing;\r
16 \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
22         }\r
23 \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
26         }\r
27 \r
28         public static MvcForm BeginForm(this HtmlHelper htmlHelper, RouteValueDictionary routeValues) {\r
29             return BeginForm(htmlHelper, null, null, routeValues, FormMethod.Post, new RouteValueDictionary());\r
30         }\r
31 \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
34         }\r
35 \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
38         }\r
39 \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
42         }\r
43 \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
46         }\r
47 \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
50         }\r
51 \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
54         }\r
55 \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
58         }\r
59 \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
62         }\r
63 \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
66         }\r
67 \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
71         }\r
72 \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
75         }\r
76 \r
77         public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, RouteValueDictionary routeValues) {\r
78             return BeginRouteForm(htmlHelper, null /* routeName */, routeValues, FormMethod.Post, new RouteValueDictionary());\r
79         }\r
80 \r
81         public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName) {\r
82             return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(), FormMethod.Post, new RouteValueDictionary());\r
83         }\r
84 \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
87         }\r
88 \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
91         }\r
92 \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
95         }\r
96 \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
99         }\r
100 \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
103         }\r
104 \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
107         }\r
108 \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
111         }\r
112 \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
115         }\r
116 \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
120         }\r
121 \r
122         public static void EndForm(this HtmlHelper htmlHelper) {\r
123             HttpResponseBase httpResponse = htmlHelper.ViewContext.HttpContext.Response;\r
124             httpResponse.Write("</form>");\r
125         }\r
126 \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
134 \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
138         }\r
139     }\r
140 }\r