Advertisement
Willcode4cash

IDisposable Html Tags

Oct 31st, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.44 KB | None | 0 0
  1. public static IDisposable Tag(this HtmlHelper html, string tagName)
  2. {
  3.     if (html == null)
  4.         throw new ArgumentNullException("html");
  5.  
  6.     Action<string> a = tag => html.Write(String.Format(tag, tagName));
  7.     a("<{0}>");
  8.     return new Memento(() => a("</{0}>"));
  9. }
  10.  
  11. // Used like
  12.  
  13. using (Html.Tag("ul"))
  14. {
  15.     this.Model.ForEach(item => using(Html.Tag("li")) Html.Write(item));
  16.     using(Html.Tag("li")) Html.Write("new");
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement