CombotreeExtensions.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Mvc;
  6. using System.Linq.Expressions;
  7. using System.Web.Mvc.Html;
  8. using Bowin.Web.Controls.Mvc;
  9. using Bowin.Web.Controls.Mvc.Utility;
  10. namespace System.Web.Mvc
  11. {
  12. public static class CombotreeExtensions
  13. {
  14. public static System.Web.Mvc.MvcHtmlString Combotree(
  15. this System.Web.Mvc.HtmlHelper htmlHelper
  16. , CombotreeOptions combotreeOptions
  17. , IDictionary<string, object> htmlAttributes = null)
  18. {
  19. return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.Combotree.CreateControl(combotreeOptions, htmlAttributes).Render());
  20. }
  21. public static MvcHtmlString CombotreeFor<TModel, TProperty>(
  22. this HtmlHelper<TModel> htmlHelper
  23. , Expression<Func<TModel, TProperty>> expression
  24. , string dataSourceUrl)
  25. {
  26. var combotreeOptions = new CombotreeOptions { DataSourceUrl = dataSourceUrl };
  27. var htmldic = AnalyzingExpression<TModel, TProperty>(htmlHelper, expression, combotreeOptions);
  28. return Combotree(htmlHelper, combotreeOptions, htmldic);
  29. }
  30. public static MvcHtmlString CombotreeFor<TModel, TProperty>(
  31. this HtmlHelper<TModel> htmlHelper
  32. , Expression<Func<TModel, TProperty>> expression
  33. , CombotreeOptions combotreeOptions)
  34. {
  35. var htmldic = AnalyzingExpression<TModel, TProperty>(htmlHelper, expression, combotreeOptions);
  36. return Combotree(htmlHelper, combotreeOptions, htmldic);
  37. }
  38. public static MvcHtmlString CombotreeFor<TModel, TProperty>(
  39. this HtmlHelper<TModel> htmlHelper
  40. , Expression<Func<TModel, TProperty>> expression
  41. , CombotreeOptions combotreeOptions
  42. , IDictionary<string, object> htmlAttributes)
  43. {
  44. AnalyzingExpression<TModel, TProperty>(htmlHelper, expression, combotreeOptions);
  45. return Combotree(htmlHelper, combotreeOptions, htmlAttributes);
  46. }
  47. private static IDictionary<string,object> AnalyzingExpression<TModel, TProperty>(HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, CombotreeOptions combotreeOptions)
  48. {
  49. if (expression == null)
  50. {
  51. throw new ArgumentNullException("expression");
  52. }
  53. ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
  54. string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
  55. combotreeOptions.Name = fullHtmlFieldName;
  56. if (modelMetadata.Model != null)
  57. {
  58. //Guid emptyg = Guid.Parse(modelMetadata.Model.ToString());
  59. //if (emptyg != Guid.Empty)
  60. //{
  61. combotreeOptions.SelectedValue = modelMetadata.Model;
  62. //}
  63. }
  64. return htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata);
  65. }
  66. }
  67. }