PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/MvcFutures/Mvc/Html/HtmlHelperExtensions.cs

#
C# | 527 lines | 408 code | 82 blank | 37 comment | 3 complexity | 05039ee731917342a88d4d93c056d4f9 MD5 | raw file
Possible License(s): Apache-2.0
  1. namespace Microsoft.Web.Mvc.Html {
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Linq.Expressions;
  6. using System.Web.Mvc;
  7. using System.Web.Mvc.Html;
  8. using System.Web.Routing;
  9. // This class contains definitions of single "super" HTML helper methods, which rely on
  10. // CLR 4's default values for method parameters to make them more consumable. Methods
  11. // which previously took an HTML attributes object/dictionary now have their legal
  12. // attribute values all available as optional parameters. Some attributes are only
  13. // applicable for some DTDs; deprecated attributes (like "align" on input) were
  14. // specifically excluded.
  15. //
  16. // Since htmlAttributes was very often the last parameter to HTML helper methods,
  17. // converting to these new syntaxes should be as simple as converting your anonymous
  18. // object htmlAttributes collections into optional parameters.
  19. //
  20. // Where there were two overloads for route values (anonymous object and dictionary),
  21. // there is only a single overload now, which takes type object. If what you pass is
  22. // a dictionary of the correct type, then we'll use that; otherwise, we'll assume it's
  23. // an anonymous object and create the dictionary for you. This should make it simple
  24. // to port methods using route values, as they should just continue to work as before.
  25. //
  26. // Some HTML helpers did not take HTML attributes parameters. They are recreated here
  27. // so that the user does not have to import both System.Web.Mvc.Html as well as this
  28. // namespace, since the purpose of these methods is to get rid of all the overloads
  29. // for the built-in HTML helpers.
  30. //
  31. // The legal attribute values were derived from: http://www.w3schools.com/tags/
  32. public static class HtmlHelperExtensions {
  33. // ChildActionExtensions
  34. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  35. public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, string controllerName = null, object routeValues = null) {
  36. return ChildActionExtensions.Action(
  37. htmlHelper,
  38. actionName,
  39. controllerName,
  40. routeValues as IDictionary<string, object> ?? new RouteValueDictionary(routeValues)
  41. );
  42. }
  43. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  44. public static void RenderAction(this HtmlHelper htmlHelper, string actionName, string controllerName = null, object routeValues = null) {
  45. ChildActionExtensions.RenderAction(
  46. htmlHelper,
  47. actionName,
  48. controllerName,
  49. routeValues as IDictionary<string, object> ?? new RouteValueDictionary(routeValues)
  50. );
  51. }
  52. // DisplayExtensions
  53. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  54. public static MvcHtmlString Display(this HtmlHelper htmlHelper, string expression, string templateName = null, string htmlFieldName = null) {
  55. return DisplayExtensions.Display(htmlHelper, expression, templateName, htmlFieldName);
  56. }
  57. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
  58. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  59. public static MvcHtmlString DisplayFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, string templateName = null, string htmlFieldName = null) {
  60. return DisplayExtensions.DisplayFor(htmlHelper, expression, templateName, htmlFieldName);
  61. }
  62. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  63. public static MvcHtmlString DisplayForModel(this HtmlHelper htmlHelper, string templateName = null, string htmlFieldName = null) {
  64. return DisplayExtensions.DisplayForModel(htmlHelper, templateName, htmlFieldName);
  65. }
  66. // DisplayTextExtensions
  67. public static MvcHtmlString DisplayText(this HtmlHelper htmlHelper, string name) {
  68. return DisplayTextExtensions.DisplayText(htmlHelper, name);
  69. }
  70. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
  71. public static MvcHtmlString DisplayTextFor<TModel, TResult>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TResult>> expression) {
  72. return DisplayTextExtensions.DisplayTextFor(htmlHelper, expression);
  73. }
  74. // EditorExtensions
  75. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  76. public static MvcHtmlString Editor(this HtmlHelper htmlHelper, string expression, string templateName = null, string htmlFieldName = null) {
  77. return EditorExtensions.Editor(htmlHelper, expression, templateName, htmlFieldName);
  78. }
  79. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
  80. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  81. public static MvcHtmlString EditorFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, string templateName = null, string htmlFieldName = null) {
  82. return EditorExtensions.EditorFor(htmlHelper, expression, templateName, htmlFieldName);
  83. }
  84. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  85. public static MvcHtmlString EditorForModel(this HtmlHelper htmlHelper, string templateName = null, string htmlFieldName = null) {
  86. return EditorExtensions.EditorForModel(htmlHelper, templateName, htmlFieldName);
  87. }
  88. // FormExtensions
  89. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  90. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Charset", Justification = "The name matches the HTML attribute name")]
  91. public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName = null, string controllerName = null, object routeValues = null, FormMethod method = FormMethod.Post, string accept = null, string acceptCharset = null, string cssClass = null, string dir = null, string encType = null, string id = null, string lang = null, string name = null, string style = null, string title = null) {
  92. return FormExtensions.BeginForm(
  93. htmlHelper,
  94. actionName,
  95. controllerName,
  96. routeValues as RouteValueDictionary ?? new RouteValueDictionary(routeValues),
  97. method,
  98. FormAttributes(accept, acceptCharset, cssClass, dir, encType, id, lang, name, style, title)
  99. );
  100. }
  101. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  102. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Charset", Justification = "The name matches the HTML attribute name")]
  103. public static MvcForm BeginRouteForm(this HtmlHelper htmlHelper, string routeName, RouteValueDictionary routeValues = null, FormMethod method = FormMethod.Post, string accept = null, string acceptCharset = null, string cssClass = null, string dir = null, string encType = null, string id = null, string lang = null, string name = null, string style = null, string title = null) {
  104. return FormExtensions.BeginRouteForm(
  105. htmlHelper,
  106. routeName,
  107. routeValues as RouteValueDictionary ?? new RouteValueDictionary(routeValues),
  108. method,
  109. FormAttributes(accept, acceptCharset, cssClass, dir, encType, id, lang, name, style, title)
  110. );
  111. }
  112. public static void EndForm(this HtmlHelper htmlHelper) {
  113. FormExtensions.EndForm(htmlHelper);
  114. }
  115. // InputExtensions
  116. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  117. public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, bool? isChecked = null, string cssClass = null, string dir = null, bool disabled = false, string id = null, string lang = null, int? maxLength = null, bool readOnly = false, int? size = null, string style = null, int? tabIndex = null, string title = null) {
  118. var htmlAttributes = InputAttributes(cssClass, dir, disabled, id, lang, maxLength, readOnly, size, style, tabIndex, title);
  119. return isChecked.HasValue
  120. ? InputExtensions.CheckBox(htmlHelper, name, isChecked.Value, htmlAttributes)
  121. : InputExtensions.CheckBox(htmlHelper, name, htmlAttributes);
  122. }
  123. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
  124. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  125. public static MvcHtmlString CheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool>> expression, string cssClass = null, string dir = null, bool disabled = false, string id = null, string lang = null, int? maxLength = null, bool readOnly = false, int? size = null, string style = null, int? tabIndex = null, string title = null) {
  126. return InputExtensions.CheckBoxFor(
  127. htmlHelper,
  128. expression,
  129. InputAttributes(cssClass, dir, disabled, id, lang, maxLength, readOnly, size, style, tabIndex, title)
  130. );
  131. }
  132. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  133. public static MvcHtmlString Hidden(this HtmlHelper htmlHelper, string name, object value = null, string cssClass = null, string id = null, string style = null) {
  134. return InputExtensions.Hidden(htmlHelper, name, value, Attributes(cssClass, id, style));
  135. }
  136. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
  137. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  138. public static MvcHtmlString HiddenFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string cssClass = null, string id = null, string style = null) {
  139. return InputExtensions.HiddenFor(htmlHelper, expression, Attributes(cssClass, id, style));
  140. }
  141. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  142. public static MvcHtmlString Password(this HtmlHelper htmlHelper, string name, object value = null, string cssClass = null, string dir = null, bool disabled = false, string id = null, string lang = null, int? maxLength = null, bool readOnly = false, int? size = null, string style = null, int? tabIndex = null, string title = null) {
  143. return InputExtensions.Password(
  144. htmlHelper,
  145. name,
  146. value,
  147. InputAttributes(cssClass, dir, disabled, id, lang, maxLength, readOnly, size, style, tabIndex, title)
  148. );
  149. }
  150. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
  151. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  152. public static MvcHtmlString PasswordFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string cssClass = null, bool disabled = false, string dir = null, string id = null, string lang = null, int? maxLength = null, bool readOnly = false, int? size = null, string style = null, int? tabIndex = null, string title = null) {
  153. return InputExtensions.PasswordFor(
  154. htmlHelper,
  155. expression,
  156. InputAttributes(cssClass, dir, disabled, id, lang, maxLength, readOnly, size, style, tabIndex, title)
  157. );
  158. }
  159. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  160. public static MvcHtmlString RadioButton(this HtmlHelper htmlHelper, string name, object value, bool? isChecked = null, string cssClass = null, string dir = null, bool disabled = false, string id = null, string lang = null, int? maxLength = null, bool readOnly = false, int? size = null, string style = null, int? tabIndex = null, string title = null) {
  161. var htmlAttributes = InputAttributes(cssClass, dir, disabled, id, lang, maxLength, readOnly, size, style, tabIndex, title);
  162. return isChecked.HasValue
  163. ? InputExtensions.RadioButton(htmlHelper, name, value, isChecked.Value, htmlAttributes)
  164. : InputExtensions.RadioButton(htmlHelper, name, value, htmlAttributes);
  165. }
  166. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
  167. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  168. public static MvcHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, string cssClass = null, string dir = null, bool disabled = false, string id = null, string lang = null, int? maxLength = null, bool readOnly = false, int? size = null, string style = null, int? tabIndex = null, string title = null) {
  169. return InputExtensions.RadioButtonFor(
  170. htmlHelper,
  171. expression,
  172. value,
  173. InputAttributes(cssClass, dir, disabled, id, lang, maxLength, readOnly, size, style, tabIndex, title)
  174. );
  175. }
  176. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  177. public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, object value = null, string cssClass = null, string dir = null, bool disabled = false, string id = null, string lang = null, int? maxLength = null, bool readOnly = false, int? size = null, string style = null, int? tabIndex = null, string title = null) {
  178. return InputExtensions.TextBox(
  179. htmlHelper,
  180. name,
  181. value,
  182. InputAttributes(cssClass, dir, disabled, id, lang, maxLength, readOnly, size, style, tabIndex, title)
  183. );
  184. }
  185. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
  186. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  187. public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string cssClass = null, string dir = null, bool disabled = false, string id = null, string lang = null, int? maxLength = null, bool readOnly = false, int? size = null, string style = null, int? tabIndex = null, string title = null) {
  188. return InputExtensions.TextBoxFor(
  189. htmlHelper,
  190. expression,
  191. InputAttributes(cssClass, dir, disabled, id, lang, maxLength, readOnly, size, style, tabIndex, title)
  192. );
  193. }
  194. // LabelExtensions
  195. public static MvcHtmlString Label(this HtmlHelper htmlHelper, string expression) {
  196. return LabelExtensions.Label(htmlHelper, expression);
  197. }
  198. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
  199. public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression) {
  200. return LabelExtensions.LabelFor(htmlHelper, expression);
  201. }
  202. public static MvcHtmlString LabelForModel(this HtmlHelper htmlHelper) {
  203. return LabelExtensions.LabelForModel(htmlHelper);
  204. }
  205. // LinkExtensions
  206. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  207. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "charset", Justification = "This matched the HTML attribute spelling")]
  208. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "coords", Justification = "This matched the HTML attribute spelling")]
  209. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "rel", Justification = "This matched the HTML attribute spelling")]
  210. public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName = null, string protocol = null, string hostName = null, string fragment = null, object routeValues = null, string accessKey = null, string charset = null, string coords = null, string cssClass = null, string dir = null, string hrefLang = null, string id = null, string lang = null, string name = null, string rel = null, string rev = null, string shape = null, string style = null, string target = null, string title = null) {
  211. return LinkExtensions.ActionLink(
  212. htmlHelper,
  213. linkText,
  214. actionName,
  215. controllerName,
  216. protocol,
  217. hostName,
  218. fragment,
  219. routeValues as RouteValueDictionary ?? new RouteValueDictionary(routeValues),
  220. AnchorAttributes(accessKey, charset, coords, cssClass, dir, hrefLang, id, lang, name, rel, rev, shape, style, target, title)
  221. );
  222. }
  223. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  224. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "charset", Justification = "The name matches the HTML attribute name")]
  225. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "coords", Justification = "The name matches the HTML attribute name")]
  226. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "rel", Justification = "The name matches the HTML attribute name")]
  227. public static MvcHtmlString RouteLink(this HtmlHelper htmlHelper, string linkText, string routeName, string protocol = null, string hostName = null, string fragment = null, object routeValues = null, string accessKey = null, string charset = null, string coords = null, string cssClass = null, string dir = null, string hrefLang = null, string id = null, string lang = null, string name = null, string rel = null, string rev = null, string shape = null, string style = null, string target = null, string title = null) {
  228. return LinkExtensions.RouteLink(
  229. htmlHelper,
  230. linkText,
  231. routeName,
  232. protocol,
  233. hostName,
  234. fragment,
  235. routeValues as RouteValueDictionary ?? new RouteValueDictionary(routeValues),
  236. AnchorAttributes(accessKey, charset, coords, cssClass, dir, hrefLang, id, lang, name, rel, rev, shape, style, target, title)
  237. );
  238. }
  239. // PartialExtensions
  240. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  241. public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName, object model = null, ViewDataDictionary viewData = null) {
  242. return PartialExtensions.Partial(
  243. htmlHelper,
  244. partialViewName,
  245. model,
  246. viewData ?? htmlHelper.ViewData
  247. );
  248. }
  249. // RenderPartialExtensions
  250. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  251. public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, object model = null, ViewDataDictionary viewData = null) {
  252. RenderPartialExtensions.RenderPartial(
  253. htmlHelper,
  254. partialViewName,
  255. model,
  256. viewData ?? htmlHelper.ViewData
  257. );
  258. }
  259. // SelectExtensions
  260. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  261. public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList = null, string optionLabel = null, string cssClass = null, string dir = null, bool disabled = false, string id = null, string lang = null, int? size = null, string style = null, int? tabIndex = null, string title = null) {
  262. return SelectExtensions.DropDownList(
  263. htmlHelper,
  264. name,
  265. selectList,
  266. optionLabel,
  267. SelectAttributes(cssClass, dir, disabled, id, lang, size, style, tabIndex, title)
  268. );
  269. }
  270. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
  271. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  272. public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList = null, string optionLabel = null, string cssClass = null, string dir = null, bool disabled = false, string id = null, string lang = null, int? size = null, string style = null, int? tabIndex = null, string title = null) {
  273. return SelectExtensions.DropDownListFor(
  274. htmlHelper,
  275. expression,
  276. selectList,
  277. optionLabel,
  278. SelectAttributes(cssClass, dir, disabled, id, lang, size, style, tabIndex, title)
  279. );
  280. }
  281. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  282. public static MvcHtmlString ListBox(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList = null, string cssClass = null, string dir = null, bool disabled = false, string id = null, string lang = null, int? size = null, string style = null, int? tabIndex = null, string title = null) {
  283. return SelectExtensions.ListBox(
  284. htmlHelper,
  285. name,
  286. selectList,
  287. SelectAttributes(cssClass, dir, disabled, id, lang, size, style, tabIndex, title)
  288. );
  289. }
  290. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
  291. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  292. public static MvcHtmlString ListBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList = null, string cssClass = null, string dir = null, bool disabled = false, string id = null, string lang = null, int? size = null, string style = null, int? tabIndex = null, string title = null) {
  293. return SelectExtensions.ListBoxFor(
  294. htmlHelper,
  295. expression,
  296. selectList,
  297. SelectAttributes(cssClass, dir, disabled, id, lang, size, style, tabIndex, title)
  298. );
  299. }
  300. // TextAreaExtensions
  301. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  302. public static MvcHtmlString TextArea(this HtmlHelper htmlHelper, string name, string value = null, string accessKey = null, string cssClass = null, int? cols = null, string dir = null, bool disabled = false, string id = null, string lang = null, bool readOnly = false, int? rows = null, string style = null, int? tabIndex = null, string title = null) {
  303. return TextAreaExtensions.TextArea(
  304. htmlHelper,
  305. name,
  306. value,
  307. TextAreaAttributes(accessKey, cssClass, cols, dir, disabled, id, lang, readOnly, rows, style, tabIndex, title)
  308. );
  309. }
  310. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
  311. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  312. public static MvcHtmlString TextAreaFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string accessKey = null, string cssClass = null, int? cols = null, string dir = null, bool disabled = false, string id = null, string lang = null, bool readOnly = false, int? rows = null, string style = null, int? tabIndex = null, string title = null) {
  313. return TextAreaExtensions.TextAreaFor(
  314. htmlHelper,
  315. expression,
  316. TextAreaAttributes(accessKey, cssClass, cols, dir, disabled, id, lang, readOnly, rows, style, tabIndex, title)
  317. );
  318. }
  319. // ValidationExtensions
  320. public static void Validate(this HtmlHelper htmlHelper, string modelName) {
  321. ValidationExtensions.Validate(htmlHelper, modelName);
  322. }
  323. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
  324. public static void ValidateFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) {
  325. ValidationExtensions.ValidateFor(htmlHelper, expression);
  326. }
  327. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  328. [SuppressMessage("Microsoft.Naming", "CA1719:ParameterNamesShouldNotMatchMemberNames", MessageId = "2#")]
  329. public static MvcHtmlString ValidationMessage(this HtmlHelper htmlHelper, string modelName, string validationMessage = null, string cssClass = null, string dir = null, string id = null, string lang = null, string style = null, string title = null) {
  330. return ValidationExtensions.ValidationMessage(
  331. htmlHelper,
  332. modelName,
  333. validationMessage,
  334. SpanAttributes(cssClass, dir, id, lang, style, title)
  335. );
  336. }
  337. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
  338. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  339. public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string validationMessage = null, string cssClass = null, string dir = null, string id = null, string lang = null, string style = null, string title = null) {
  340. return ValidationExtensions.ValidationMessageFor(
  341. htmlHelper,
  342. expression,
  343. validationMessage,
  344. SpanAttributes(cssClass, dir, id, lang, style, title)
  345. );
  346. }
  347. [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed", Justification = "The purpose of these helpers is to use default parameters to simplify common usage.")]
  348. public static MvcHtmlString ValidationSummary(this HtmlHelper htmlHelper, string message = null, bool excludePropertyErrors = false, string cssClass = null, string dir = null, string id = null, string lang = null, string style = null, string title = null) {
  349. return ValidationExtensions.ValidationSummary(
  350. htmlHelper,
  351. excludePropertyErrors,
  352. message,
  353. SpanAttributes(cssClass, dir, id, lang, style, title)
  354. );
  355. }
  356. // Helper methods
  357. private static void AddOptional(this IDictionary<string, object> dictionary, string key, bool value) {
  358. if (value) {
  359. dictionary[key] = key;
  360. }
  361. }
  362. private static void AddOptional(this IDictionary<string, object> dictionary, string key, object value) {
  363. if (value != null) {
  364. dictionary[key] = value;
  365. }
  366. }
  367. private static IDictionary<string, object> Attributes(string cssClass, string id, string style) {
  368. var htmlAttributes = new RouteValueDictionary();
  369. htmlAttributes.AddOptional("class", cssClass);
  370. htmlAttributes.AddOptional("id", id);
  371. htmlAttributes.AddOptional("style", style);
  372. return htmlAttributes;
  373. }
  374. private static IDictionary<string, object> AnchorAttributes(string accessKey, string charset, string coords, string cssClass, string dir, string hrefLang, string id, string lang, string name, string rel, string rev, string shape, string style, string target, string title) {
  375. var htmlAttributes = Attributes(cssClass, id, style);
  376. htmlAttributes.AddOptional("accesskey", accessKey);
  377. htmlAttributes.AddOptional("charset", charset);
  378. htmlAttributes.AddOptional("coords", coords);
  379. htmlAttributes.AddOptional("dir", dir);
  380. htmlAttributes.AddOptional("hreflang", hrefLang);
  381. htmlAttributes.AddOptional("lang", lang);
  382. htmlAttributes.AddOptional("name", name);
  383. htmlAttributes.AddOptional("rel", rel);
  384. htmlAttributes.AddOptional("rev", rev);
  385. htmlAttributes.AddOptional("shape", shape);
  386. htmlAttributes.AddOptional("target", target);
  387. htmlAttributes.AddOptional("title", title);
  388. return htmlAttributes;
  389. }
  390. private static IDictionary<string, object> FormAttributes(string accept, string acceptCharset, string cssClass, string dir, string encType, string id, string lang, string name, string style, string title) {
  391. var htmlAttributes = Attributes(cssClass, id, style);
  392. htmlAttributes.AddOptional("accept", accept);
  393. htmlAttributes.AddOptional("accept-charset", acceptCharset);
  394. htmlAttributes.AddOptional("dir", dir);
  395. htmlAttributes.AddOptional("enctype", encType);
  396. htmlAttributes.AddOptional("lang", lang);
  397. htmlAttributes.AddOptional("name", name);
  398. htmlAttributes.AddOptional("title", title);
  399. return htmlAttributes;
  400. }
  401. private static IDictionary<string, object> InputAttributes(string cssClass, string dir, bool disabled, string id, string lang, int? maxLength, bool readOnly, int? size, string style, int? tabIndex, string title) {
  402. var htmlAttributes = Attributes(cssClass, id, style);
  403. htmlAttributes.AddOptional("dir", dir);
  404. htmlAttributes.AddOptional("disabled", disabled);
  405. htmlAttributes.AddOptional("lang", lang);
  406. htmlAttributes.AddOptional("maxlength", maxLength);
  407. htmlAttributes.AddOptional("readonly", readOnly);
  408. htmlAttributes.AddOptional("size", size);
  409. htmlAttributes.AddOptional("tabindex", tabIndex);
  410. htmlAttributes.AddOptional("title", title);
  411. return htmlAttributes;
  412. }
  413. private static IDictionary<string, object> SelectAttributes(string cssClass, string dir, bool disabled, string id, string lang, int? size, string style, int? tabIndex, string title) {
  414. var htmlAttributes = Attributes(cssClass, id, style);
  415. htmlAttributes.AddOptional("dir", dir);
  416. htmlAttributes.AddOptional("disabled", disabled);
  417. htmlAttributes.AddOptional("lang", lang);
  418. htmlAttributes.AddOptional("size", size);
  419. htmlAttributes.AddOptional("tabindex", tabIndex);
  420. htmlAttributes.AddOptional("title", title);
  421. return htmlAttributes;
  422. }
  423. private static IDictionary<string, object> SpanAttributes(string cssClass, string dir, string id, string lang, string style, string title) {
  424. var htmlAttributes = Attributes(cssClass, id, style);
  425. htmlAttributes.AddOptional("dir", dir);
  426. htmlAttributes.AddOptional("lang", lang);
  427. htmlAttributes.AddOptional("title", title);
  428. return htmlAttributes;
  429. }
  430. private static IDictionary<string, object> TextAreaAttributes(string accessKey, string cssClass, int? cols, string dir, bool disabled, string id, string lang, bool readOnly, int? rows, string style, int? tabIndex, string title) {
  431. var htmlAttributes = Attributes(cssClass, id, style);
  432. htmlAttributes.AddOptional("accesskey", accessKey);
  433. htmlAttributes.AddOptional("cols", cols);
  434. htmlAttributes.AddOptional("dir", dir);
  435. htmlAttributes.AddOptional("disabled", disabled);
  436. htmlAttributes.AddOptional("lang", lang);
  437. htmlAttributes.AddOptional("readonly", readOnly);
  438. htmlAttributes.AddOptional("rows", rows);
  439. htmlAttributes.AddOptional("style", style);
  440. htmlAttributes.AddOptional("tabindex", tabIndex);
  441. htmlAttributes.AddOptional("title", title);
  442. return htmlAttributes;
  443. }
  444. }
  445. }