在我们的项目中经常会因为一个新闻的标题过长而不想全部显示。下面提供一个截断文本的类。
在MVC目录下新建一个名为 Extersions 的文件夹,在该文件夹中新建一个截断文本类,取名为:CutOfTextExtersions
该类代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace System.Web.Mvc //修改为所属System.Web.Mvc命名空间 方便直接使用
{
/// <summary>
/// 截取字符串类
/// </summary>
public static class CutOfTextExtersions
{
/// <summary>
/// 截取字符串方法
/// </summary>
/// <param name="helper"></param>
/// <param name="str">字符串</param>
/// <param name="len">长度</param>
/// <param name="flag">是否显示。。。</param>
/// <returns></returns>
public static string GotTopic(this HtmlHelper helper, string str, int len, bool flag)
{
if (str != null && str != "")
{
string clearstr = str.RemoveHTML();
int count = 0;
string strTemp = "";
for (int i = 0; i < clearstr.Length; i++)
{
if (Math.Abs(((int)(clearstr.Substring(i, 1).ToCharArray())[0])) > 255)
count += 2;
else
count += 1;
if (count <= len)
strTemp += clearstr.Substring(i, 1);
else
{
strTemp = strTemp + (flag == true ? "…" : "");
return str.Replace(clearstr, strTemp);
}
}
return str.Replace(clearstr, strTemp).Replace(" ", "").Trim();
}
else
return "";
}
public static string RemoveHTML(this string str)
{
try
{
if (str != "")
{
str = System.Text.RegularExpressions.Regex.Replace(str, "<[^>]*>", "");
str = str.Replace(" ", " ");
return str;
}
else
return "";
}
catch
{
return "";
}
}
}
}
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace System.Web.Mvc //修改为所属System.Web.Mvc命名空间 方便直接使用
{
/// <summary>
/// 截取字符串类
/// </summary>
public static class CutOfTextExtersions
{
/// <summary>
/// 截取字符串方法
/// </summary>
/// <param name="helper"></param>
/// <param name="str">字符串</param>
/// <param name="len">长度</param>
/// <param name="flag">是否显示。。。</param>
/// <returns></returns>
public static string GotTopic(this HtmlHelper helper, string str, int len, bool flag)
{
if (str != null && str != "")
{
string clearstr = str.RemoveHTML();
int count = 0;
string strTemp = "";
for (int i = 0; i < clearstr.Length; i++)
{
if (Math.Abs(((int)(clearstr.Substring(i, 1).ToCharArray())[0])) > 255)
count += 2;
else
count += 1;
if (count <= len)
strTemp += clearstr.Substring(i, 1);
else
{
strTemp = strTemp + (flag == true ? "…" : "");
return str.Replace(clearstr, strTemp);
}
}
return str.Replace(clearstr, strTemp).Replace(" ", "").Trim();
}
else
return "";
}
public static string RemoveHTML(this string str)
{
try
{
if (str != "")
{
str = System.Text.RegularExpressions.Regex.Replace(str, "<[^>]*>", "");
str = str.Replace(" ", " ");
return str;
}
else
return "";
}
catch
{
return "";
}
}
}
}
在View中使用该类:
@Html.ActionLink(@Html.GotTopic(@item.NewsTitle, 40, true))
@Html.ActionLink(@Html.GotTopic(@item.NewsTitle, 40, false))
相关推荐
MVC3处理反复提交 HtmlHelper用法大全 拓展方法
MVC_HtmlHelper用法大全,主要用于学习CSHTML中MVC标签!
Asp.net + MVC 日历控件 HtmlHelper
ASP.NET MVC 分页 HtmlHelper控件2010贺岁版 by Herman.格式分为四种: .1:上一页/下一页 模式; .2:首页/尾页/上一页/下一页 模式; .3:上一页 1 2 3 4 5 下一页 模式; .4:总共X条, 当前1/X页,首页 上一页 1 2 3 4 5 ...
在MVC3 中给HtmlHelper 添加扩展(Extension)来消除魔鬼代码.
许多时候我们会遇到如下场景 在写一个编辑数据的页面时,我们通常会写如下代码 由前篇我们所讲的Helper演化,我们思考,对于这种...这当然是可以的,ASP.NET MVC提供了一个HtmlHelper用于生成有数据绑定的Html标签。
ASP.NET MVC HtmlHelper用法大全ASP.NET MVC HtmlHelper用法大全
ASP.NET MVC框架开发系列课程(20):HTML Helper的使用与扩展
1、 将sd8686.bin和sd8686_helper.bin拷贝到linux-2.6.35/firmware下 2、 平台代码修改,修改arch/arm/mach-s3c2410/mach-smdk2410.c 3、 配置内核 4、 编译内核
IPHelper5.2 - 增加了修改计算机名称及执行自定义命令等功能。修正了部分bug,增进界面友好性。(2008/12) IPHelper4.6 - 增加了“生成快捷方式”功能,大大提高了部分常用配置的切换速度。软件可以 最小化到任务...
内含 整合架包android-PullToRefresh-Library和实例代码MVCHelper_Demo。 github地址:https://github.com/LuckyJayce/MVCHelper 实现下拉刷新,滚动底部自动加载更多,分页加载,自动切换显示网络失败布局,暂无...
MVC 3 – Razor View Engine By Jon Galloway|April 4, 2011 In this video you will learn the basics of the new Razor view engine included in ASP.NET MVC 3. MVC 3 – Razor Layouts By Jon Galloway|April 4...
ASP.NET MVC 中HtmlHelper方法为我们提供很多html标签,只需在页面调用就行了,但是微软并没有把所有的html标签都对应有了扩展方法,需要我们自定义HtmlHelper,来满足我们需要。
/// A simple helper demonstrating the @Html.Raw /// </summary> public IEncodedString Raw(string rawString) { return new RawString(rawString); } public int ToInt(string str) { int returnValue...
DataTables.AspnetCore.Mvc为数据表提供htmlHelper包装器。 NUGET 最简单的安装方法是使用 。 当前版本是用netstandard2.0编写的 如何 有关选项的详细信息,请参考官方文档。 依存关系 DataTables只有一个库依赖 ...
Aspnet Mvc教程 3. 建立并运行 02:46 Aspnet Mvc教程 4.默认程序结构 04:18 Aspnet Mvc教程 5. 基本工作流程 05:13 Aspnet Mvc教程 6.mvc理论讲解 03:53 Aspnet Mvc教程 7.ViewData 04:51 Aspnet Mvc教程 8....
Asp.Net MVC4 + Oracle + EasyUI + Bootstrap 第一章 --操作数据 1、 关于HtmlHelper和UrlHelper 因为大部分的web请求的目标都是向客户端发送HTML代码。因此,Asp.Net MVC也是 为我们创建HTML提供了各种帮助。...
主要介绍了asp.net core razor自定义taghelper的方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
JUtils 自定义 Java Helper 类