(Razor) Html.Helper 中的字符串长度?
这是一个非常简单的问题。
我有一个 Html.helper:
@Html.DisplayFor(modelItem => item.Text)
如何将 item.Text 中的字符串削减到特定长度?我希望您可以直接在 item.Text 上执行 SubString
或其他操作。
如果您想知道为什么我想要这个,那是因为字符串很长,我只想在索引视图等中显示其中的一部分。
This is a very simple question.
I have a Html.helper:
@Html.DisplayFor(modelItem => item.Text)
How to I cut down the string from item.Text to a specific length? I wish you could do a SubString
or something directly on the item.Text.
If youre wondering why I want this, its because the strings are very long, and I only want to show a bit of it in like the index view etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可以考虑 3 种可能性:
There are 3 possibilities that could be considered:
我需要同样的东西并用以下几行解决了这个问题。
如果你的字符串总是大于10,你可以排除:
并直接写:
另外我建议为大于你给出的限制的字符串添加
..
。I needed the same thing and solved the case with the following lines.
If your string is always larger than 10, you can rule out:
And directly write:
Also I recommend adding
..
for strings larger than the limit you give.您可以只在视图模型中添加一个属性来截断字符串并显示它:
You could just add a property onto your view model that does the truncation of the string and display that instead:
更改
为
Change
to
编辑:新答案
关于
DisplayFor 的原型是:
我认为 modelItem 是动态的,因此应该可以向视图模型添加新属性。
Edited : New Answer
what about
The prototype for DisplayFor is :
And the modelItem is a dynamic I think, so it should be possible to add anew property to the view model.