Advertisement
Guest User

Untitled

a guest
Nov 12th, 2012
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. using System.Web.Mvc;
  2.  
  3. namespace HtmlHelperExtensions
  4. {
  5.     public static class HtmlHelperExtensions
  6.     {
  7.         public static MvcHtmlString If(this MvcHtmlString value, bool evaluation)
  8.         {
  9.             return evaluation ? value : MvcHtmlString.Empty;
  10.         }
  11.  
  12.         public static MvcHtmlString AddMenuItem(this HtmlHelper html, string action, string imagePath, string alt)
  13.         {
  14.             var url = new UrlHelper(html.ViewContext.RequestContext);
  15.  
  16.             // build the <img> tag
  17.             var imgBuilder = new TagBuilder("img");
  18.             imgBuilder.MergeAttribute("src", url.Content(imagePath));
  19.             imgBuilder.MergeAttribute("alt", alt);
  20.             string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);
  21.  
  22.             // build the <a> tag
  23.             var anchorBuilder = new TagBuilder("a");
  24.             anchorBuilder.MergeAttribute("href", url.Action(action));
  25.             anchorBuilder.InnerHtml = imgHtml + "<br />" + action; // include the <img> tag inside
  26.             string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);
  27.  
  28.             return MvcHtmlString.Create(anchorHtml);
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement