c# – 如何使用MVC Html Helpers截断字符串?

栏目: ASP.NET · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/23572631/how-can-i-truncate-a-string-using-mvc-html-helpers

我试图截断一个长字符串只在我的索引页面上显示.它显示如下:

<td>
    @Html.DisplayFor(modelItem => item.Description)
</td>

描述可以是500个字符长,但我不能在该网格布局上显示那么多.我只想显示前25个,因为他们可以在详细信息页面上看到所有这些,但我似乎无法让它在模型级别截断它.

像这样的东西会很好:

@Html.DisplayFor(modelItem => item.Description.Take(25))
@Html.DisplayFor(modelItem => item.Description.Substring(0,25)

编辑

当我尝试任一方法时,我在运行时遇到以下异常.

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

不要使用html帮助器.这样做:

@item.Description.Substring(0, Math.Min(item.Description.Length, 25));

我假设你在某个循环中item是当前元素.

翻译自:https://stackoverflow.com/questions/23572631/how-can-i-truncate-a-string-using-mvc-html-helpers


以上所述就是小编给大家介绍的《c# – 如何使用MVC Html Helpers截断字符串?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Distributed Systems

Distributed Systems

Sukumar Ghosh / Chapman and Hall/CRC / 2014-7-14 / USD 119.95

Distributed Systems: An Algorithmic Approach, Second Edition provides a balanced and straightforward treatment of the underlying theory and practical applications of distributed computing. As in the p......一起来看看 《Distributed Systems》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具