Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Web.Mvc;
- namespace HtmlHelperExtensions
- {
- public static class HtmlHelperExtensions
- {
- public static MvcHtmlString If(this MvcHtmlString value, bool evaluation)
- {
- return evaluation ? value : MvcHtmlString.Empty;
- }
- public static MvcHtmlString AddMenuItem(this HtmlHelper html, string action, string imagePath, string alt)
- {
- var url = new UrlHelper(html.ViewContext.RequestContext);
- // build the <img> tag
- var imgBuilder = new TagBuilder("img");
- imgBuilder.MergeAttribute("src", url.Content(imagePath));
- imgBuilder.MergeAttribute("alt", alt);
- string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);
- // build the <a> tag
- var anchorBuilder = new TagBuilder("a");
- anchorBuilder.MergeAttribute("href", url.Action(action));
- anchorBuilder.InnerHtml = imgHtml + "<br />" + action; // include the <img> tag inside
- string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);
- return MvcHtmlString.Create(anchorHtml);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement