MVC3 自定义HtmlHelper截断文本内容
1、取一个字符串和一个最大长度,如果字符串小于最大长度,就直接返回这个字符串,如果大于最大长度,那么截断字符串,被截断的字符串以 “…“表示 2、使用HtmlHelper使专辑名小于16个字符 3、多个视图中使用HtmlHelper
View Code using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.WebPages.Html; namespace MVC3MusicStore.Helpers { /// <summary> /// 自定义HtmlHelper截断文本内容 /// </summary> public static class HtmlHelpers { public static string Truncate(this HtmlHelper helper, string input, int length) { if (input.Length <= length) { return input; } else { return input.Substring(0, length) + "..."; } } } }
重启VS智能提示