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.Ajax {
\r
15 using System.Collections.Generic;
\r
16 using System.ComponentModel;
\r
17 using System.Globalization;
\r
19 using System.Web.Mvc;
\r
20 using System.Web.Mvc.Html;
\r
21 using System.Web.Mvc.Resources;
\r
22 using System.Web.Routing;
\r
24 public static class AjaxExtensions {
\r
25 private const string LinkOnClickFormat = "Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), {0});";
\r
26 private const string FormOnSubmitFormat = "Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event), {0});";
\r
28 public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, AjaxOptions ajaxOptions) {
\r
29 return ActionLink(ajaxHelper, linkText, actionName, (string)null /* controllerName */, ajaxOptions);
\r
32 public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, object routeValues, AjaxOptions ajaxOptions) {
\r
33 return ActionLink(ajaxHelper, linkText, actionName, (string)null /* controllerName */, routeValues, ajaxOptions);
\r
36 public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
\r
37 return ActionLink(ajaxHelper, linkText, actionName, (string)null /* controllerName */, routeValues, ajaxOptions, htmlAttributes);
\r
40 public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {
\r
41 return ActionLink(ajaxHelper, linkText, actionName, (string)null /* controllerName */, routeValues, ajaxOptions);
\r
44 public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
\r
45 return ActionLink(ajaxHelper, linkText, actionName, (string)null /* controllerName */, routeValues, ajaxOptions, htmlAttributes);
\r
48 public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, AjaxOptions ajaxOptions) {
\r
49 return ActionLink(ajaxHelper, linkText, actionName, controllerName, null /* values */, ajaxOptions, null /* htmlAttributes */);
\r
52 public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions) {
\r
53 return ActionLink(ajaxHelper, linkText, actionName, controllerName, routeValues, ajaxOptions, null /* htmlAttributes */);
\r
56 public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
\r
57 RouteValueDictionary newValues = new RouteValueDictionary(routeValues);
\r
58 Dictionary<string, object> newAttributes = ObjectToCaseSensitiveDictionary(htmlAttributes);
\r
59 return ActionLink(ajaxHelper, linkText, actionName, controllerName, newValues, ajaxOptions, newAttributes);
\r
62 public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {
\r
63 return ActionLink(ajaxHelper, linkText, actionName, controllerName, routeValues, ajaxOptions, null /* htmlAttributes */);
\r
66 public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
\r
67 if (String.IsNullOrEmpty(linkText)) {
\r
68 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
\r
71 string targetUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true /* includeImplicitMvcValues */);
\r
73 return GenerateLink(linkText, targetUrl, GetAjaxOptions(ajaxOptions), htmlAttributes);
\r
76 public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
\r
77 RouteValueDictionary newValues = new RouteValueDictionary(routeValues);
\r
78 Dictionary<string, object> newAttributes = ObjectToCaseSensitiveDictionary(htmlAttributes);
\r
79 return ActionLink(ajaxHelper, linkText, actionName, controllerName, protocol, hostName, fragment, newValues, ajaxOptions, newAttributes);
\r
82 public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
\r
83 if (String.IsNullOrEmpty(linkText)) {
\r
84 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
\r
87 string targetUrl = UrlHelper.GenerateUrl(null /* routeName */, actionName, controllerName, protocol, hostName, fragment, routeValues, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true /* includeImplicitMvcValues */);
\r
89 return GenerateLink(linkText, targetUrl, ajaxOptions, htmlAttributes);
\r
92 public static MvcForm BeginForm(this AjaxHelper ajaxHelper, AjaxOptions ajaxOptions) {
\r
93 string formAction = ajaxHelper.ViewContext.HttpContext.Request.RawUrl;
\r
95 TagBuilder builder = new TagBuilder("form");
\r
97 builder.MergeAttribute("action", formAction);
\r
98 builder.MergeAttribute("method", "post");
\r
99 builder.MergeAttribute("onsubmit", GenerateAjaxScript(GetAjaxOptions(ajaxOptions), FormOnSubmitFormat));
\r
101 HttpResponseBase response = ajaxHelper.ViewContext.HttpContext.Response;
\r
102 response.Write(builder.ToString(TagRenderMode.StartTag));
\r
103 return new MvcForm(response);
\r
106 public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, AjaxOptions ajaxOptions) {
\r
107 return BeginForm(ajaxHelper, actionName, (string)null /* controllerName */, ajaxOptions);
\r
110 public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, object routeValues, AjaxOptions ajaxOptions) {
\r
111 return BeginForm(ajaxHelper, actionName, (string)null /* controllerName */, routeValues, ajaxOptions);
\r
114 public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
\r
115 return BeginForm(ajaxHelper, actionName, (string)null /* controllerName */, routeValues, ajaxOptions, htmlAttributes);
\r
118 public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {
\r
119 return BeginForm(ajaxHelper, actionName, (string)null /* controllerName */, routeValues, ajaxOptions);
\r
122 public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
\r
123 return BeginForm(ajaxHelper, actionName, (string)null /* controllerName */, routeValues, ajaxOptions, htmlAttributes);
\r
126 public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, AjaxOptions ajaxOptions) {
\r
127 return BeginForm(ajaxHelper, actionName, controllerName, null /* values */, ajaxOptions, null /* htmlAttributes */);
\r
130 public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions) {
\r
131 return BeginForm(ajaxHelper, actionName, controllerName, routeValues, ajaxOptions, null /* htmlAttributes */);
\r
134 public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
\r
135 RouteValueDictionary newValues = new RouteValueDictionary(routeValues);
\r
136 Dictionary<string, object> newAttributes = ObjectToCaseSensitiveDictionary(htmlAttributes);
\r
137 return BeginForm(ajaxHelper, actionName, controllerName, newValues, ajaxOptions, newAttributes);
\r
140 public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {
\r
141 return BeginForm(ajaxHelper, actionName, controllerName, routeValues, ajaxOptions, null /* htmlAttributes */);
\r
144 public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
\r
146 string formAction = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues ?? new RouteValueDictionary(), ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true /* includeImplicitMvcValues */);
\r
147 return FormHelper(ajaxHelper, formAction, ajaxOptions, htmlAttributes);
\r
150 public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, AjaxOptions ajaxOptions) {
\r
151 return BeginRouteForm(ajaxHelper, routeName, null /* routeValues */, ajaxOptions, null /* htmlAttributes */);
\r
154 public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, object routeValues, AjaxOptions ajaxOptions) {
\r
155 return BeginRouteForm(ajaxHelper, routeName, (object)routeValues, ajaxOptions, null /* htmlAttributes */);
\r
158 public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
\r
159 Dictionary<string, object> newAttributes = ObjectToCaseSensitiveDictionary(htmlAttributes);
\r
160 return BeginRouteForm(ajaxHelper, routeName, new RouteValueDictionary(routeValues), ajaxOptions, newAttributes);
\r
163 public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {
\r
164 return BeginRouteForm(ajaxHelper, routeName, routeValues, ajaxOptions, null /* htmlAttributes */);
\r
167 public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
\r
168 string formAction = UrlHelper.GenerateUrl(routeName, null /* actionName */, null /* controllerName */, routeValues ?? new RouteValueDictionary(), ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, false /* includeImplicitMvcValues */);
\r
169 return FormHelper(ajaxHelper, formAction, ajaxOptions, htmlAttributes);
\r
172 private static MvcForm FormHelper(this AjaxHelper ajaxHelper, string formAction, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
\r
173 TagBuilder builder = new TagBuilder("form");
\r
174 builder.MergeAttributes(htmlAttributes);
\r
175 builder.MergeAttribute("action", formAction);
\r
176 builder.MergeAttribute("method", "post");
\r
177 builder.MergeAttribute("onsubmit", GenerateAjaxScript(GetAjaxOptions(ajaxOptions), FormOnSubmitFormat));
\r
179 HttpResponseBase response = ajaxHelper.ViewContext.HttpContext.Response;
\r
180 response.Write(builder.ToString(TagRenderMode.StartTag));
\r
181 return new MvcForm(response);
\r
184 public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, object routeValues, AjaxOptions ajaxOptions) {
\r
185 return RouteLink(ajaxHelper, linkText, null /* routeName */, new RouteValueDictionary(routeValues), ajaxOptions,
\r
186 new Dictionary<string, object>());
\r
189 public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
\r
190 return RouteLink(ajaxHelper, linkText, null /* routeName */, new RouteValueDictionary(routeValues), ajaxOptions,
\r
191 ObjectToCaseSensitiveDictionary(htmlAttributes));
\r
194 public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {
\r
195 return RouteLink(ajaxHelper, linkText, null /* routeName */, routeValues, ajaxOptions,
\r
196 new Dictionary<string, object>());
\r
199 public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
\r
200 return RouteLink(ajaxHelper, linkText, null /* routeName */, routeValues, ajaxOptions, htmlAttributes);
\r
203 public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, AjaxOptions ajaxOptions) {
\r
204 return RouteLink(ajaxHelper, linkText, routeName, new RouteValueDictionary(), ajaxOptions,
\r
205 new Dictionary<string, object>());
\r
208 public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, AjaxOptions ajaxOptions, object htmlAttributes) {
\r
209 return RouteLink(ajaxHelper, linkText, routeName, new RouteValueDictionary(), ajaxOptions, ObjectToCaseSensitiveDictionary(htmlAttributes));
\r
212 public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
\r
213 return RouteLink(ajaxHelper, linkText, routeName, new RouteValueDictionary(), ajaxOptions, htmlAttributes);
\r
216 public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, object routeValues, AjaxOptions ajaxOptions) {
\r
217 return RouteLink(ajaxHelper, linkText, routeName, new RouteValueDictionary(routeValues), ajaxOptions,
\r
218 new Dictionary<string, object>());
\r
221 public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
\r
222 return RouteLink(ajaxHelper, linkText, routeName, new RouteValueDictionary(routeValues), ajaxOptions,
\r
223 ObjectToCaseSensitiveDictionary(htmlAttributes));
\r
226 public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {
\r
227 return RouteLink(ajaxHelper, linkText, routeName, routeValues, ajaxOptions, new Dictionary<string, object>());
\r
230 public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
\r
231 if (String.IsNullOrEmpty(linkText)) {
\r
232 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
\r
235 string targetUrl = UrlHelper.GenerateUrl(routeName, null /* actionName */, null /* controllerName */, routeValues ?? new RouteValueDictionary(), ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, false /* includeImplicitMvcValues */);
\r
237 return GenerateLink(linkText, targetUrl, GetAjaxOptions(ajaxOptions), htmlAttributes);
\r
240 public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
\r
241 if (String.IsNullOrEmpty(linkText)) {
\r
242 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
\r
245 string targetUrl = UrlHelper.GenerateUrl(routeName, null /* actionName */, null /* controllerName */, protocol, hostName, fragment, routeValues ?? new RouteValueDictionary(), ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, false /* includeImplicitMvcValues */);
\r
247 return GenerateLink(linkText, targetUrl, GetAjaxOptions(ajaxOptions), htmlAttributes);
\r
250 internal static string InsertionModeToString(InsertionMode insertionMode) {
\r
251 switch (insertionMode) {
\r
252 case InsertionMode.Replace:
\r
253 return "Sys.Mvc.InsertionMode.replace";
\r
254 case InsertionMode.InsertBefore:
\r
255 return "Sys.Mvc.InsertionMode.insertBefore";
\r
256 case InsertionMode.InsertAfter:
\r
257 return "Sys.Mvc.InsertionMode.insertAfter";
\r
259 return ((int)insertionMode).ToString(CultureInfo.InvariantCulture);
\r
263 private static Dictionary<string, object> ObjectToCaseSensitiveDictionary(object values) {
\r
264 Dictionary<string, object> dict = new Dictionary<string, object>(StringComparer.Ordinal);
\r
265 if (values != null) {
\r
266 foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(values)) {
\r
267 object val = prop.GetValue(values);
\r
268 dict[prop.Name] = val;
\r
274 private static string GenerateLink(string linkText, string targetUrl, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
\r
275 TagBuilder tag = new TagBuilder("a") {
\r
276 InnerHtml = HttpUtility.HtmlEncode(linkText)
\r
279 tag.MergeAttributes(htmlAttributes);
\r
280 tag.MergeAttribute("href", targetUrl);
\r
281 tag.MergeAttribute("onclick", GenerateAjaxScript(ajaxOptions, LinkOnClickFormat));
\r
283 return tag.ToString(TagRenderMode.Normal);
\r
286 private static string GenerateAjaxScript(AjaxOptions ajaxOptions, string scriptFormat) {
\r
287 string optionsString = ajaxOptions.ToJavascriptString();
\r
288 return String.Format(CultureInfo.InvariantCulture, scriptFormat, optionsString);
\r
291 private static AjaxOptions GetAjaxOptions(AjaxOptions ajaxOptions) {
\r
292 return (ajaxOptions != null) ? ajaxOptions : new AjaxOptions();
\r