Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 9th, 2012  |  syntax: None  |  size: 5.46 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. MVC3 HtmlHelpers using Generics and LINQ
  2. @model List<MyProject.MyModels.UserModel>
  3. @(Html.MyExtensions()
  4.       .PrintUsers(Model)
  5.       .FirstName(m => m.FirstName)
  6.       .LastName(m => m.LastName)
  7. )
  8.        
  9. @model List<MyProject.MyModels.UserModel>
  10. @(Html.MyExtensions<List<MyProject.MyModels.UserModel>, MyProject.MyModels.UserModel>()
  11.       .PrintUsers(Model)
  12.       .FirstName(m => m.FirstName)
  13.       .LastName(m => m.LastName)
  14. )
  15.        
  16. public class UserModel
  17. {
  18.     public string FirstName { get; set; }
  19.     public string LastName { get; set; }
  20. }
  21.        
  22. public static class UIExtension
  23. {
  24.     public static ComponentFactory<TModel, T> MyExtensions<TModel, T>(this HtmlHelper<TModel> html)
  25.         where TModel : IEnumerable<T>
  26.         where T : class
  27.     {
  28.         return new ComponentFactory<TModel, T>(html);
  29.     }
  30. }
  31.        
  32. public class ComponentFactory<TModel, T>
  33.     where T : class
  34. {
  35.     private HtmlHelper<TModel> _html;
  36.  
  37.     public ComponentFactory()
  38.     {
  39.     }
  40.  
  41.     public ComponentFactory(HtmlHelper<TModel> html)
  42.     {
  43.         this._html = html;
  44.     }
  45.  
  46.     public ListComponent<T> PrintUsers(IEnumerable<T> model)
  47.     {
  48.         return new ListComponent<T>(model);
  49.     }
  50.  
  51.     /* Add other ui components to this factory */
  52. }
  53.        
  54. public class ListComponent<T> : IHtmlString
  55. {
  56.     private Expression<Func<T, string>> _firstname;
  57.     private Expression<Func<T, string>> _lastname;
  58.  
  59.     private IEnumerable<T> _model;
  60.  
  61.     public ListComponent(IEnumerable<T> model)
  62.     {
  63.         this._model = model;
  64.     }
  65.  
  66.     public ListComponent<T> FirstName(Expression<Func<T, string>> property)
  67.     {
  68.         this._firstname = property;
  69.         return this;
  70.     }
  71.  
  72.     public ListComponent<T> LastName(Expression<Func<T, string>> property)
  73.     {
  74.         this._lastname = property;
  75.         return this;
  76.     }
  77.  
  78.     public override MvcHtmlString Render()
  79.     {
  80.         TagBuilder container = new TagBuilder("div");
  81.  
  82.         TagBuilder title = new TagBuilder("div");
  83.         title.InnerHtml = "<b>Names</b>";
  84.         container.InnerHtml = title.ToString(TagRenderMode.Normal);
  85.  
  86.         TagBuilder ul = new TagBuilder("ul");
  87.  
  88.         foreach (var item in this._model)
  89.         {
  90.             TagBuilder li = new TagBuilder("li");
  91.             li.SetInnerText(String.Format("{0} {1}", _firstname.Compile()(item), _lastname.Compile()(item)));
  92.             ul.InnerHtml += li.ToString(TagRenderMode.Normal);
  93.         }
  94.  
  95.         container.InnerHtml += ul.ToString(TagRenderMode.Normal);
  96.  
  97.         return MvcHtmlString.Create(container.ToString(TagRenderMode.Normal));
  98.     }
  99. }
  100.        
  101. public static class UIExtension
  102. {
  103.     public static ComponentFactory<TModel> MyExtensions<TModel>(this HtmlHelper<IEnumerable<TModel>> html) where TModel : class
  104.  
  105.     {
  106.         return new ComponentFactory<TModel>(html);
  107.     }
  108. }
  109. public class UserModel
  110. {
  111.     public string FirstName { get; set; }
  112.     public string LastName { get; set; }
  113. }
  114. public class ComponentFactory<TModel>
  115. where TModel : class
  116. {
  117.     private HtmlHelper _html;
  118.  
  119.     public ComponentFactory()
  120.     {
  121.     }
  122.  
  123.     public ComponentFactory(HtmlHelper<IEnumerable<TModel>> html)
  124.     {
  125.         this._html = html;
  126.     }
  127.  
  128.     public ListComponent<TModel> PrintUsers(IEnumerable<TModel> model)
  129.     {
  130.         return new ListComponent<TModel>(model);
  131.     }
  132.  
  133.     /* Add other ui components to this factory */
  134. }
  135. public class ListComponent<T> : IHtmlString
  136. {
  137.     private Expression<Func<T, string>> _firstname;
  138.     private Expression<Func<T, string>> _lastname;
  139.  
  140.     private IEnumerable<T> _model;
  141.  
  142.     public ListComponent(IEnumerable<T> model)
  143.     {
  144.         this._model = model;
  145.     }
  146.  
  147.     public ListComponent<T> FirstName(Expression<Func<T, string>> property)
  148.     {
  149.         this._firstname = property;
  150.         return this;
  151.     }
  152.  
  153.     public ListComponent<T> LastName(Expression<Func<T, string>> property)
  154.     {
  155.         this._lastname = property;
  156.         return this;
  157.     }
  158.  
  159.  
  160.  
  161. public override MvcHtmlString Render()
  162. {
  163.     TagBuilder container = new TagBuilder("div");
  164.  
  165.     TagBuilder title = new TagBuilder("div");
  166.     title.InnerHtml = "<b>Names</b>";
  167.     container.InnerHtml = title.ToString(TagRenderMode.Normal);
  168.  
  169.     TagBuilder ul = new TagBuilder("ul");
  170.  
  171.     foreach (var item in this._model)
  172.     {
  173.         TagBuilder li = new TagBuilder("li");
  174.         li.SetInnerText(String.Format("{0} {1}", _firstname.Compile()(item), _lastname.Compile()(item)));
  175.         ul.InnerHtml += li.ToString(TagRenderMode.Normal);
  176.     }
  177.  
  178.     container.InnerHtml += ul.ToString(TagRenderMode.Normal);
  179.  
  180.     return MvcHtmlString.Create(container.ToString(TagRenderMode.Normal));
  181. }
  182. }
  183.        
  184. Html.MyExtensions().PrintUsers(Model).FirstName(p=>p.FirstName).LastName(p=>p.LastName)
  185.        
  186. public static class UIExtension
  187. {
  188.    public static ComponentFactory MyExtensions(this HtmlHelper html)
  189.    {
  190.        return new ComponentFactory(html);
  191.    }
  192.  
  193.    public static ComponentFactory<TModel> MyExtensions<TModel>(this HtmlHelper<TModel> html)
  194.       where TModel : class
  195.    {
  196.        return new ComponentFactory<TModel>(html);
  197.    }
  198. }
  199.        
  200. public class ComponentFactory<TModel>
  201. {
  202.    private HtmlHelper<TModel> _html;
  203.    public ComponentFactory(HtmlHelper<TModel> html)
  204.    {
  205.       this._html = html;
  206.    }
  207.  
  208.    public ListComponent<TObj> PrintUsers<TObj>(IEnumerable<TObj> model)
  209.    {
  210.       return new ListComponent<TObj>(model);
  211.    }
  212. }
  213.        
  214. @(Html.MyExtensions()
  215.       .PrintUsers(Model.MyList)
  216.       .FirstName(m => m.FirstName)
  217.       .LastName(m => m.LastName)
  218. )
  219.        
  220. ListComponent<TList, TItem> Print<TList, TItem>(TList list) where TList : IEnumerable<TItem>
  221.        
  222. ListComponent<TItem> Print<TItem>(IEnumerable<TItem> list)