Language: C#
Html Helpers
1: public enum StyleConditions { 2: IE6OrLess, IE7OrLess 3: } 4: 5: public static class HtmlHelpers { 6: #region Image 7: public static string Image(this HtmlHelper helper, string route, string action, int id, int height, int width, int imageType, 8: string altText, string cssClass) { 9: var urlHelper = new UrlHelper(helper.ViewContext.RequestContext); 10: var imageUrl = urlHelper.RouteUrl(route, new RouteValueDictionary(new { 11: action = action, 12: id = id, 13: height = height, 14: width = width, 15: imageType = imageType 16: })); 17: 18: var imgTag = new TagBuilder("img"); 19: imgTag.MergeAttribute("src", imageUrl.ToLower()); 20: imgTag.MergeAttribute("height", height.ToString()); 21: imgTag.MergeAttribute("width", width.ToString()); 22: 23: if (altText != null) 24: imgTag.MergeAttribute("alt", altText); 25: 26: if (cssClass != null) 27: imgTag.AddCssClass(cssClass); 28: 29: return imgTag.ToString(TagRenderMode.SelfClosing); 30: } 31: #endregion 32: 33: #region Meta 34: public static string MetaTag(this HtmlHelper helper, string name, string content) { 35: var metaTag = new TagBuilder("meta"); 36: metaTag.MergeAttribute("content", content); 37: metaTag.MergeAttribute("name", name); 38: 39: return metaTag.ToString(TagRenderMode.SelfClosing); 40: } 41: #endregion 42: 43: #region Style Tag 44: public static string StyleTag(this HtmlHelper helper, string href, string condition) { 45: var styleTag = new TagBuilder("link"); 46: styleTag.MergeAttribute("href", href.ToLower()); 47: styleTag.MergeAttribute("rel", "stylesheet"); 48: styleTag.MergeAttribute("type", "text/css"); 49: 50: var html = styleTag.ToString(TagRenderMode.SelfClosing); 51: if (condition != null) 52: html = String.Format("<!--[if {0}]>{1}<![endif]-->", condition, html); 53: 54: return html; 55: } 56: 57: public static string StyleTag(this HtmlHelper helper, string href, StyleConditions styleCondition) { 58: string condition; 59: switch (styleCondition) { 60: case StyleConditions.IE6OrLess: 61: condition = "lt IE 7"; 62: break; 63: case StyleConditions.IE7OrLess: 64: condition = "lt IE 8"; 65: break; 66: default: 67: throw new NotImplementedException("StyleCondition not implemented."); 68: } 69: 70: return StyleTag(helper, href, condition); 71: } 72: 73: public static string StyleTag(this HtmlHelper helper, string href) { 74: return StyleTag(helper, href, null); 75: } 76: #endregion 77: 78: #region Script Tag 79: public static string ScriptTag(this HtmlHelper helper, string src) { 80: var scriptTag = new TagBuilder("script"); 81: scriptTag.MergeAttribute("src", src.ToLower()); 82: scriptTag.MergeAttribute("type", "text/javascript"); 83: 84: return scriptTag.ToString(TagRenderMode.Normal); 85: } 86: #endregion 87: 88: #region Recaptcha 89: public static string GenerateCaptcha(this HtmlHelper helper, string theme, string publicKey, string privateKey) { 90: if (helper.ViewContext.HttpContext.Request.IsAjaxRequest()) 91: return helper.GenerateAjaxCaptcha(theme, publicKey); 92: 93: var captchaControl = new RecaptchaControl { 94: ID = "recaptcha", 95: Theme = theme, 96: PublicKey = publicKey, 97: PrivateKey = privateKey 98: }; 99: 100: var htmlWriter = new HtmlTextWriter(new StringWriter()); 101: captchaControl.RenderControl(htmlWriter); 102: 103: return htmlWriter.InnerWriter.ToString(); 104: } 105: 106: public static string GenerateAjaxCaptcha(this HtmlHelper helper, string theme, string publicKey) { 107: var htmlWriter = new HtmlTextWriter(new StringWriter()); 108: 109: htmlWriter.WriteLine("<script src=\"http://api.recaptcha.net/js/recaptcha_ajax.js\" type=\"text/javascript\"/>"); 110: htmlWriter.WriteLine("<div id=\"recaptchaAjax\"></div>"); 111: htmlWriter.WriteLine("<script type=\"text/javascript\">"); 112: htmlWriter.WriteLine("Recaptcha.create(\"" + publicKey + "\", \"recaptchaAjax\", { theme: \"" + theme + "\" });"); 113: htmlWriter.WriteLine("</script>"); 114: 115: return htmlWriter.InnerWriter.ToString(); 116: } 117: #endregion 118: 119: #region Repeater 120: // Based on http://haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net-mvc.aspx 121: 122: public static void Repeater<T>(this HtmlHelper html, IList<T> items, 123: Action<T, string> render, Action renderIfEmpty, string cssClass, string cssClassAlt) { 124: if (items.IsNullOrEmpty()) { 125: renderIfEmpty(); 126: } 127: else { 128: int i = 0; 129: items.ForEach(item => { 130: render(item, (i++ % 2 == 0) ? cssClass : cssClassAlt); 131: }); 132: } 133: } 134: 135: public static void Repeater<T>(this HtmlHelper html, IList<T> items, 136: Action<T, string> render, Action renderIfEmpty) { 137: Repeater<T>(html, items, render, renderIfEmpty, "", "alt"); 138: } 139: 140: public static void Repeater<T>(this HtmlHelper html, string viewDataKey, 141: Action<T, string> render, Action renderIfEmpty, string cssClass, string cssClassAlt) { 142: var items = GetViewDataAsList<T>(html, viewDataKey); 143: Repeater<T>(html, items, render, renderIfEmpty, cssClass, cssClassAlt); 144: } 145: 146: public static void Repeater<T>(this HtmlHelper html, string viewDataKey, 147: Action<T, string> render, Action renderIfEmpty) { 148: var items = GetViewDataAsList<T>(html, viewDataKey); 149: Repeater<T>(html, items, render, renderIfEmpty); 150: } 151: 152: static IList<T> GetViewDataAsList<T>(HtmlHelper html, string viewDataKey) { 153: var items = html.ViewContext.ViewData as IList<T>; 154: var viewData = html.ViewContext.ViewData as IDictionary<string, object>; 155: if (viewData != null) { 156: items = viewData[viewDataKey] as IList<T>; 157: } 158: else { 159: items = new ViewDataDictionary(viewData)[viewDataKey] 160: as IList<T>; 161: } 162: return items; 163: } 164: #endregion 165: }
Tags:
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

