asp.net-mvc - 如何在 MVC 中进行子字符串化。我在这里有什么错?
问题描述
如何在 MVC 中使用子字符串?
@if (i.Aciklama.Length > 50)
{
@Html.Raw(i.Aciklama.Substring(0,50))...
}
else
{
@Html.Raw(i.Aciklama)
}
解决方案
我猜 Aciklama 是字符串类型的
@Html.DisplayFor(model => model.Aciklama).ToString().Substring(0,50)
或者使用扩展
namespace YourApp.Extensions
{
public static class StringExtensions
{
public static string Truncate(this string input, int max)
{
if(!String.IsNullOrEmpty(input) && input.Length > max)
{
return input.Substring(0,max);
}
}
}
}
在您看来,使用 YourApp.Extensions 添加;
@Model.Aciklama.Truncate(50)
推荐阅读
- javascript - 为什么 runkit.com 代码返回的值与本地代码不同?
- flutter - Flutter Cloud Firestore - 是否可以为客户端禁用网络?
- c++ - “...已经在 *.obj 中定义”但我没有在同一范围内定义任何 var 两次
- php - 如何在实时服务器上部署静态子域 + 通配符子域 laravel 应用程序?
- python - 使用 Tkinter 在 Python 中插入 JPG 图像
- wso2 - 仍然可重现:未发现此服务器中部署的工件。忽略碳应用
- javafx - 检测到 DragOver 事件时使用哪个 CSS 选择器?
- ruby - 清理 URL 字符串
- javascript - 如何将字符串转换为blob?
- html - 如何在 Bootstrap 3 输入组中使按钮全宽?