PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Trunk/MvcExtensions/ExtensionMethod/ViewDataDictionaryExtensions.cs

http://mvcextensions.codeplex.com
C# | 159 lines | 69 code | 20 blank | 70 comment | 3 complexity | a9c72338d1fdb6ec2a9c5399e69131b2 MD5 | raw file
Possible License(s): BSD-3-Clause, CC-BY-SA-3.0, Apache-2.0
  1. #region Copyright
  2. // Copyright (c) 2009 - 2010, Kazi Manzur Rashid <kazimanzurrashid@gmail.com>.
  3. // This source is subject to the Microsoft Public License.
  4. // See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  5. // All other rights reserved.
  6. #endregion
  7. namespace MvcExtensions
  8. {
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Web.Mvc;
  12. using System.Web.Script.Serialization;
  13. /// <summary>
  14. /// Defines an static class which contains extension methods of <see cref="ViewDataDictionary"/>.
  15. /// </summary>
  16. public static class ViewDataDictionaryExtensions
  17. {
  18. /// <summary>
  19. /// Gets the value against the specified type name.
  20. /// </summary>
  21. /// <typeparam name="TValue">The type of the value.</typeparam>
  22. /// <param name="instance">The instance.</param>
  23. /// <returns></returns>
  24. public static TValue Get<TValue>(this ViewDataDictionary instance)
  25. {
  26. return Get<TValue>(instance, MakeKey<TValue>());
  27. }
  28. /// <summary>
  29. /// Gets the value that is stored against the specified key.
  30. /// </summary>
  31. /// <typeparam name="TValue">The type of the value.</typeparam>
  32. /// <param name="instance">The instance.</param>
  33. /// <param name="key">The key.</param>
  34. /// <returns></returns>
  35. public static TValue Get<TValue>(this ViewDataDictionary instance, string key)
  36. {
  37. return Get(instance, key, default(TValue));
  38. }
  39. /// <summary>
  40. /// Gets the value that is stored against the specified key, if the key does not exists it will return the provided default value.
  41. /// </summary>
  42. /// <typeparam name="TValue">The type of the value.</typeparam>
  43. /// <param name="instance">The instance.</param>
  44. /// <param name="key">The key.</param>
  45. /// <param name="defaultValue">The default value.</param>
  46. /// <returns></returns>
  47. public static TValue Get<TValue>(this ViewDataDictionary instance, string key, TValue defaultValue)
  48. {
  49. Invariant.IsNotNull(instance, "instance");
  50. return instance.ContainsKey(key) ? (TValue)instance[key] : defaultValue;
  51. }
  52. /// <summary>
  53. /// Sets the value against the specified type.
  54. /// </summary>
  55. /// <typeparam name="TValue">The type of the value.</typeparam>
  56. /// <param name="instance">The instance.</param>
  57. /// <param name="value">The value.</param>
  58. public static void Set<TValue>(this ViewDataDictionary instance, TValue value)
  59. {
  60. Set(instance, MakeKey<TValue>(), value);
  61. }
  62. /// <summary>
  63. /// Sets the value against the specified key.
  64. /// </summary>
  65. /// <typeparam name="TValue">The type of the value.</typeparam>
  66. /// <param name="instance">The instance.</param>
  67. /// <param name="key">The key.</param>
  68. /// <param name="value">The value.</param>
  69. public static void Set<TValue>(this ViewDataDictionary instance, string key, TValue value)
  70. {
  71. Invariant.IsNotNull(instance, "instance");
  72. instance[key] = value;
  73. }
  74. /// <summary>
  75. /// Determines whether the specified type exists.
  76. /// </summary>
  77. /// <typeparam name="TValue">The type of the value.</typeparam>
  78. /// <param name="instance">The instance.</param>
  79. /// <returns>
  80. /// <c>true</c> if [contains] [the specified instance]; otherwise, <c>false</c>.
  81. /// </returns>
  82. public static bool Contains<TValue>(this ViewDataDictionary instance)
  83. {
  84. Invariant.IsNotNull(instance, "instance");
  85. return instance.ContainsKey(MakeKey<TValue>());
  86. }
  87. /// <summary>
  88. /// Removes the specified type.
  89. /// </summary>
  90. /// <typeparam name="TValue">The type of the value.</typeparam>
  91. /// <param name="instance">The instance.</param>
  92. public static bool Remove<TValue>(this ViewDataDictionary instance)
  93. {
  94. Invariant.IsNotNull(instance, "instance");
  95. return instance.Remove(MakeKey<TValue>());
  96. }
  97. /// <summary>
  98. /// Convert the view data into json string.
  99. /// </summary>
  100. /// <param name="instance">The instance.</param>
  101. /// <returns></returns>
  102. public static string ToJson(this ViewDataDictionary instance)
  103. {
  104. return ToJson(instance, null);
  105. }
  106. /// <summary>
  107. /// Convert the view data into json string.
  108. /// </summary>
  109. /// <param name="instance">The instance.</param>
  110. /// <param name="jsonConverters">The json converters.</param>
  111. /// <returns></returns>
  112. public static string ToJson(this ViewDataDictionary instance, IEnumerable<JavaScriptConverter> jsonConverters)
  113. {
  114. return AsSerializable(instance).ToJson(jsonConverters);
  115. }
  116. /// <summary>
  117. /// Convert the view data into a serializable object.
  118. /// </summary>
  119. /// <param name="instance">The instance.</param>
  120. /// <returns></returns>
  121. public static object AsSerializable(this ViewDataDictionary instance)
  122. {
  123. Invariant.IsNotNull(instance, "instance");
  124. var viewData = instance.Select(pair => new { key = pair.Key, value = pair.Value })
  125. .ToList();
  126. var modelStates = instance.ModelState.IsValid ?
  127. null :
  128. instance.ModelState.Select(ms => new { key = ms.Key, errors = ms.Value.Errors.Select(error => (error.Exception == null) ? error.ErrorMessage : error.Exception.Message).Where(error => !string.IsNullOrEmpty(error)) })
  129. .Where(ms => ms.errors.Any()) // No need to include model state that does not have any errors
  130. .ToList();
  131. var result = new { viewData = viewData.Any() ? viewData : null, model = instance.Model, modelStates = ((modelStates != null) && modelStates.Any()) ? modelStates : null };
  132. return result;
  133. }
  134. private static string MakeKey<TValue>()
  135. {
  136. return typeof(TValue).FullName;
  137. }
  138. }
  139. }