我怎样才能截断使用MVC HTML辅助字符串? [英] 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)
修改
我得到在运行时出现以下异常,当我尝试两种方法。
模板只能与现场访问,访问属性,一维数组的索引,或单参数自定义索引表达式中使用。
不要使用HTML辅助只要做到这一点:
@ item.Description.Substring(0,Math.Min(item.Description.Length,25));
我假设你是在一些循环,其中项目
是当前元素。
I am trying to truncate a long string for display only on my index page. It is shown like so:
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
The Description can be 500 characters long but I can't show that much on that grid layout. I'd like to show just the first 25 as they can see all of it on the Details page but I cannot seem to get it to work with out truncating it at the model level.
Something like this would be nice:
@Html.DisplayFor(modelItem => item.Description.Take(25))
@Html.DisplayFor(modelItem => item.Description.Substring(0,25)
EDIT
I'm getting the following exception at Runtime when I try either method.
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
Don't use the html helper. Just do this:
@item.Description.Substring(0, Math.Min(item.Description.Length, 25));
I'm assuming you are in some loop where item
is the current element.
这篇关于我怎样才能截断使用MVC HTML辅助字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!