PageRenderTime 89ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/CarProject/App_Code/EXTS.cs

https://gitlab.com/mh8281/CarProjectR1
C# | 232 lines | 175 code | 48 blank | 9 comment | 10 complexity | e373dbe8fb0d158998cab635d136ce3c MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using System.Web.Mvc.Html;
  8. using System.Linq.Expressions;
  9. namespace CarProject.App_Code
  10. {
  11. public static class UsefullExtensions
  12. {
  13. public static MvcHtmlString AddClassOnErr<TM, TP>(this HtmlHelper<TM> hh, Expression<Func<TM, TP>> exp, string Class)
  14. {
  15. var state = hh.ViewData.ModelState[hh.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(exp))];
  16. if (state == null || state.Errors.Count <= 0)
  17. return MvcHtmlString.Empty;
  18. else
  19. return new MvcHtmlString(Class);
  20. }
  21. #region inputs
  22. public static void AddAttribute(IDictionary<string, object> dic, string atrname, object value)
  23. {
  24. var x = dic.Where(an => an.Key.ToLower() == atrname.ToLower()).FirstOrDefault().Key;
  25. if (string.IsNullOrWhiteSpace(x))
  26. dic.Add(atrname, value);
  27. else
  28. dic[x] = dic[x].ToString() + " " + value.ToString();
  29. }
  30. public static string DicAtrsToString(IDictionary<string, object> dic)
  31. {
  32. string res = "";
  33. foreach (var item in dic)
  34. {
  35. res += string.Format(" {0}=\"{1}\" ", item.Key, item.Value.ToString());
  36. }
  37. return res;
  38. }
  39. public static MvcHtmlString input_TextBox<tm, tp>(this HtmlHelper<tm> htmlHelper, Expression<Func<tm, tp>> expression, string displayName, IDictionary<string, object> htmlAttributes, string errorClass)
  40. {
  41. if (!htmlHelper.ViewData.ModelState.IsValidField(htmlHelper.NameFor(expression).ToString()))
  42. AddAttribute(htmlAttributes, "class", errorClass);
  43. AddAttribute(htmlAttributes, "placeholder", displayName);
  44. string res = "<section class=\"input\">";
  45. res += htmlHelper.LabelFor(expression, displayName);
  46. res += htmlHelper.TextBoxFor(expression, htmlAttributes);
  47. res += htmlHelper.ValidationMessageFor(expression);
  48. res += "</section>";
  49. return new MvcHtmlString(res);
  50. }
  51. public static MvcHtmlString input_TextBox<TM, TP>(this HtmlHelper<TM> htmlHelper, Expression<Func<TM, TP>> expres, string displayName, string errorClass)
  52. {
  53. return input_TextBox<TM, TP>(htmlHelper, expres, displayName, new Dictionary<string, object>(), errorClass);
  54. }
  55. public static MvcHtmlString input_TextBox<TM, TP>(this HtmlHelper<TM> htmlHelper, Expression<Func<TM, TP>> expres, string displayName)
  56. {
  57. return input_TextBox<TM, TP>(htmlHelper, expres, displayName, new Dictionary<string, object>(), "error");
  58. }
  59. public static MvcHtmlString input_TextBox<TM, TP>(this HtmlHelper<TM> htmlHelper, Expression<Func<TM, TP>> expres, string displayName, IDictionary<string, object> htmlAttributes)
  60. {
  61. return input_TextBox<TM, TP>(htmlHelper, expres, displayName, htmlAttributes, "error");
  62. }
  63. public static MvcHtmlString input_TextArea<tm, tp>(this HtmlHelper<tm> htmlHelper, Expression<Func<tm, tp>> expression, string displayName, IDictionary<string, object> htmlAttributes, string errorClass)
  64. {
  65. if (!htmlHelper.ViewData.ModelState.IsValidField(htmlHelper.NameFor(expression).ToString()))
  66. AddAttribute(htmlAttributes, "class", errorClass);
  67. AddAttribute(htmlAttributes, "placeholder", displayName);
  68. string res = "<section class=\"input\">";
  69. res += htmlHelper.LabelFor(expression, displayName);
  70. res += htmlHelper.TextAreaFor(expression, htmlAttributes);
  71. res += htmlHelper.ValidationMessageFor(expression);
  72. res += "</section>";
  73. return new MvcHtmlString(res);
  74. }
  75. public static MvcHtmlString input_TextArea<TM, TP>(this HtmlHelper<TM> htmlHelper, Expression<Func<TM, TP>> expres, string displayName, string errorClass)
  76. {
  77. return input_TextArea<TM, TP>(htmlHelper, expres, displayName, new Dictionary<string, object>(), errorClass);
  78. }
  79. public static MvcHtmlString input_TextArea<TM, TP>(this HtmlHelper<TM> htmlHelper, Expression<Func<TM, TP>> expres, string displayName)
  80. {
  81. return input_TextArea<TM, TP>(htmlHelper, expres, displayName, new Dictionary<string, object>(), "error");
  82. }
  83. public static MvcHtmlString input_TextArea<TM, TP>(this HtmlHelper<TM> htmlHelper, Expression<Func<TM, TP>> expres, string displayName, IDictionary<string, object> htmlAttributes)
  84. {
  85. return input_TextArea<TM, TP>(htmlHelper, expres, displayName, htmlAttributes, "error");
  86. }
  87. public static MvcHtmlString input_ComboBox<tm, tp>(this HtmlHelper<tm> htmlHelper, Expression<Func<tm, tp>> expression, string displayName, IList<string> Items, IDictionary<string, object> htmlAttributes, string errorClass)
  88. {
  89. if (!htmlHelper.ViewData.ModelState.IsValidField(htmlHelper.NameFor(expression).ToString()))
  90. AddAttribute(htmlAttributes, "class", errorClass);
  91. List<SelectListItem> items = new List<SelectListItem>();
  92. items.Add(new SelectListItem { Text = "--انتخاب کنید--", Value = null, Selected = true });
  93. foreach (var item in Items)
  94. {
  95. items.Add(new SelectListItem { Text = item, Value = item });
  96. }
  97. string res = "<section class=\"input\">";
  98. res += htmlHelper.LabelFor(expression, displayName);
  99. res += htmlHelper.DropDownListFor(expression, items, htmlAttributes);
  100. /*res += string.Format("<select {0} {1} {2} >", htmlHelper.IdFor(expression).ToString(), htmlHelper.NameFor(expression).ToString(), attributes);
  101. res += string.Format("<option value=\"\" >{0}</option>", "--انتخاب کنید--");
  102. foreach (var item in Items)
  103. {
  104. res += string.Format("<option value=\"{0}\" >{0}</option>", item);
  105. }
  106. res += "</select>";*/
  107. res += htmlHelper.ValidationMessageFor(expression);
  108. res += "</section>";
  109. return new MvcHtmlString(res);
  110. }
  111. public static MvcHtmlString input_ComboBox<TM, TP>(this HtmlHelper<TM> htmlHelper, Expression<Func<TM, TP>> expres, string displayName, IList<string> Items, string errorClass)
  112. {
  113. return input_ComboBox<TM, TP>(htmlHelper, expres, displayName, Items, new Dictionary<string, object>(), errorClass);
  114. }
  115. public static MvcHtmlString input_ComboBox<TM, TP>(this HtmlHelper<TM> htmlHelper, Expression<Func<TM, TP>> expres, string displayName, IList<string> Items)
  116. {
  117. return input_ComboBox<TM, TP>(htmlHelper, expres, displayName, Items, new Dictionary<string, object>(), "error");
  118. }
  119. public static MvcHtmlString input_ComboBox<TM, TP>(this HtmlHelper<TM> htmlHelper, Expression<Func<TM, TP>> expres, string displayName, IList<string> Items, IDictionary<string, object> htmlAttributes)
  120. {
  121. return input_ComboBox<TM, TP>(htmlHelper, expres, displayName, Items, htmlAttributes, "error");
  122. }
  123. public static MvcHtmlString input_CheckBox<tm>(this HtmlHelper<tm> htmlHelper, Expression<Func<tm, bool?>> expression, string displayName, IDictionary<string, object> htmlAttributes, string errorClass)
  124. {
  125. if (!htmlHelper.ViewData.ModelState.IsValidField(htmlHelper.NameFor(expression).ToString()))
  126. AddAttribute(htmlAttributes, "class", errorClass);
  127. string res = "<section class=\"input\">";
  128. res += htmlHelper.LabelFor(expression, displayName);
  129. res += htmlHelper.CheckBox(htmlHelper.NameFor(expression).ToString(), htmlAttributes);
  130. res += htmlHelper.ValidationMessageFor(expression);
  131. res += "</section>";
  132. return new MvcHtmlString(res);
  133. }
  134. public static MvcHtmlString input_CheckBox<TM>(this HtmlHelper<TM> htmlHelper, Expression<Func<TM, bool?>> expres, string displayName, string errorClass)
  135. {
  136. return input_CheckBox<TM>(htmlHelper, expres, displayName, new Dictionary<string, object>(), errorClass);
  137. }
  138. public static MvcHtmlString input_CheckBox<TM>(this HtmlHelper<TM> htmlHelper, Expression<Func<TM, bool?>> expres, string displayName)
  139. {
  140. return input_CheckBox<TM>(htmlHelper, expres, displayName, new Dictionary<string, object>(), "error");
  141. }
  142. public static MvcHtmlString input_CheckBox<TM>(this HtmlHelper<TM> htmlHelper, Expression<Func<TM, bool?>> expres, string displayName, IDictionary<string, object> htmlAttributes)
  143. {
  144. return input_CheckBox<TM>(htmlHelper, expres, displayName, htmlAttributes, "error");
  145. }
  146. public static MvcHtmlString input_Rating<tm, tp>(this HtmlHelper<tm> htmlHelper, Expression<Func<tm, tp>> expression, string displayName, IDictionary<string, object> htmlAttributes, string errorClass)
  147. {
  148. if (!htmlHelper.ViewData.ModelState.IsValidField(htmlHelper.NameFor(expression).ToString()))
  149. AddAttribute(htmlAttributes, "class", errorClass);
  150. string cls = DicAtrsToString(htmlAttributes);
  151. string res = "<section class=\"input\">";
  152. res += htmlHelper.LabelFor(expression, displayName);
  153. {
  154. res += string.Format("<section class=\"input rating {0}\" >", cls);
  155. {
  156. res += htmlHelper.TextBoxFor(expression, new Dictionary<string, object>() { { "onKeyPress", "return floatNumber(event);" }, { "maxlength", "5" }, { "readonly", "readonly" } });
  157. res += "<section class=\"ratingSection\" onClick=\"ratint_Onclick(event)\" onMouseMove=\"ratint_OnMouseMove(event)\"> " +
  158. "<section class=\"ratingSectionSlider\" ></section> " +
  159. "<section class=\"ratingSectionFakeSlider\" ></section> " +
  160. "</section>";
  161. }
  162. res += "</section>";
  163. }
  164. res += htmlHelper.ValidationMessageFor(expression);
  165. res += "</section>";
  166. return new MvcHtmlString(res);
  167. }
  168. public static MvcHtmlString input_Rating<TM, TP>(this HtmlHelper<TM> htmlHelper, Expression<Func<TM, TP>> expres, string displayName, string errorClass)
  169. {
  170. return input_Rating<TM, TP>(htmlHelper, expres, displayName, new Dictionary<string, object>(), errorClass);
  171. }
  172. public static MvcHtmlString input_Rating<TM, TP>(this HtmlHelper<TM> htmlHelper, Expression<Func<TM, TP>> expres, string displayName)
  173. {
  174. return input_Rating<TM, TP>(htmlHelper, expres, displayName, new Dictionary<string, object>(), "error");
  175. }
  176. public static MvcHtmlString input_Rating<TM, TP>(this HtmlHelper<TM> htmlHelper, Expression<Func<TM, TP>> expres, string displayName, IDictionary<string, object> htmlAttributes)
  177. {
  178. return input_Rating<TM, TP>(htmlHelper, expres, displayName, htmlAttributes, "error");
  179. }
  180. }
  181. #endregion
  182. }