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
16 using System.Globalization;
\r
18 public static class FormExtensions {
\r
19 public static MvcForm BeginForm(this HtmlHelper htmlHelper) {
\r
20 // generates <form action="{current url}" method="post">...</form>
\r
21 string formAction = htmlHelper.ViewContext.HttpContext.Request.RawUrl;
\r
22 return FormHelper(htmlHelper, formAction, FormMethod.Post, new RouteValueDictionary());
\r
25 public static MvcForm BeginForm(this HtmlHelper htmlHelper, object routeValues) {
\r
26 return BeginForm(htmlHelper, null, null, new RouteValueDictionary(routeValues), FormMethod.Post, new RouteValueDictionary());
\r
29 public static MvcForm BeginForm(this HtmlHelper htmlHelper, RouteValueDictionary routeValues) {
\r
30 return BeginForm(htmlHelper, null, null, routeValues, FormMethod.Post, new RouteValueDictionary());
\r
33 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName) {
\r
34 return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(), FormMethod.Post, new RouteValueDictionary());
\r
37 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues) {
\r
38 return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(routeValues), FormMethod.Post, new RouteValueDictionary());
\r
41 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues) {
\r
42 return BeginForm(htmlHelper, actionName, controllerName, routeValues, FormMethod.Post, new RouteValueDictionary());
\r
45 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method) {
\r
46 return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(), method, new RouteValueDictionary());
\r
49 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method) {
\r
50 return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(routeValues), method, new RouteValueDictionary());
\r
53 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method) {
\r
54 return BeginForm(htmlHelper, actionName, controllerName, routeValues, method, new RouteValueDictionary());
\r
57 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttributes) {
\r
58 return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(), method, new RouteValueDictionary(htmlAttributes));
\r
61 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, IDictionary<string, object> htmlAttributes) {
\r
62 return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(), method, htmlAttributes);
\r
65 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method, object htmlAttributes) {
\r
66 return BeginForm(htmlHelper, actionName, controllerName, new RouteValueDictionary(routeValues), method, new RouteValueDictionary(htmlAttributes));
\r
69 public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method, IDictionary<string, object> htmlAttributes) {
\r
70 string formAction = UrlHelper.GenerateUrl(null /* routeName */, actionName, controllerName, routeValues, htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true /* includeImplicitMvcValues */);
\r
71 return FormHelper(htmlHelper, formAction, method, htmlAttributes);
\r
74 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, object routeValues) {
\r
75 return BeginRouteForm(htmlHelper, null /* routeName */, new RouteValueDictionary(routeValues), FormMethod.Post, new RouteValueDictionary());
\r
78 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, RouteValueDictionary routeValues) {
\r
79 return BeginRouteForm(htmlHelper, null /* routeName */, routeValues, FormMethod.Post, new RouteValueDictionary());
\r
82 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName) {
\r
83 return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(), FormMethod.Post, new RouteValueDictionary());
\r
86 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, object routeValues) {
\r
87 return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(routeValues), FormMethod.Post, new RouteValueDictionary());
\r
90 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, RouteValueDictionary routeValues) {
\r
91 return BeginRouteForm(htmlHelper, routeName, routeValues, FormMethod.Post, new RouteValueDictionary());
\r
94 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, FormMethod method) {
\r
95 return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(), method, new RouteValueDictionary());
\r
98 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, object routeValues, FormMethod method) {
\r
99 return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(routeValues), method, new RouteValueDictionary());
\r
102 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, RouteValueDictionary routeValues, FormMethod method) {
\r
103 return BeginRouteForm(htmlHelper, routeName, routeValues, method, new RouteValueDictionary());
\r
106 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, FormMethod method, object htmlAttributes) {
\r
107 return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(), method, new RouteValueDictionary(htmlAttributes));
\r
110 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, FormMethod method, IDictionary<string, object> htmlAttributes) {
\r
111 return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(), method, htmlAttributes);
\r
114 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, object routeValues, FormMethod method, object htmlAttributes) {
\r
115 return BeginRouteForm(htmlHelper, routeName, new RouteValueDictionary(routeValues), method, new RouteValueDictionary(htmlAttributes));
\r
118 public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, RouteValueDictionary routeValues, FormMethod method, IDictionary<string, object> htmlAttributes) {
\r
119 string formAction = UrlHelper.GenerateUrl(routeName, null, null, routeValues, htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, false /* includeImplicitMvcValues */);
\r
120 return FormHelper(htmlHelper, formAction, method, htmlAttributes);
\r
123 public static void EndForm(this HtmlHelper htmlHelper) {
\r
124 htmlHelper.ViewContext.Writer.Write("</form>");
\r
125 htmlHelper.ViewContext.OutputClientValidation();
\r
128 private static MvcForm FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> htmlAttributes) {
\r
129 TagBuilder tagBuilder = new TagBuilder("form");
\r
130 tagBuilder.MergeAttributes(htmlAttributes);
\r
131 // action is implicitly generated, so htmlAttributes take precedence.
\r
132 tagBuilder.MergeAttribute("action", formAction);
\r
133 // method is an explicit parameter, so it takes precedence over the htmlAttributes.
\r
134 tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true);
\r
136 if (htmlHelper.ViewContext.ClientValidationEnabled) {
\r
137 // forms must have an ID for client validation
\r
138 tagBuilder.GenerateId(htmlHelper.ViewContext.FormIdGenerator());
\r
141 htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag));
\r
142 MvcForm theForm = new MvcForm(htmlHelper.ViewContext);
\r
144 if (htmlHelper.ViewContext.ClientValidationEnabled) {
\r
145 htmlHelper.ViewContext.FormContext.FormId = tagBuilder.Attributes["id"];
\r