Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用MVC Html助手截断字符串?_C#_.net_Asp.net Mvc - Fatal编程技术网

C# 如何使用MVC Html助手截断字符串?

C# 如何使用MVC Html助手截断字符串?,c#,.net,asp.net-mvc,C#,.net,Asp.net Mvc,我试图截断一个长字符串,以便仅在索引页上显示。如图所示: <td> @Html.DisplayFor(modelItem => item.Description) </td> 编辑 当我尝试任何一种方法时,在运行时都会出现以下异常 Templates can be used only with field access, property access, single-dimension array index, or single-parameter c

我试图截断一个长字符串,以便仅在索引页上显示。如图所示:

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

当我尝试任何一种方法时,在运行时都会出现以下异常

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
是当前元素。

您可以使用扩展方法来实现这一点

public static string Truncate(this string source, int length)
{
    if (source.Length > length)
    {
        source = source.Substring(0, length);
    }

    return source;
}
那么在你看来,

@item.Description.Truncate(25)

您可以考虑为需要的实例创建一个特殊的模型属性:

public class MyModel
{
    public string MyDescription {get; set;}
    public string MyShortDescription {
        get 
        {
              return Truncate(MyDescription, 25);
        }
}

private string Truncate(string, howMany)
{
   // Code to perform the substring here
}

@Html.DisplayFor(modelItem => item.MyShortDescription);

您可以在数据到达视图之前截断数据,或者使用以下工具:

@{
    var shortDescript = String.Concat(modelItem.Take(25));
}
@Html.DisplayFor(modelItem => shortDescript)
试试分机

public static string TruncateMiddle(this string value, int lengthExpected, string separator = "...")
    {
        if (value.Length <= lengthExpected) return value;

        decimal sepLen = separator.Length;
        decimal charsToShow = lengthExpected - sepLen;
        decimal frontChars = Math.Ceiling(charsToShow / 2);
        decimal backChars = Math.Floor(charsToShow / 2);

        return value.Substring(0, (int)frontChars) + separator + value.Substring(value.Length - (int)backChars);            
    }

返回如下内容:Lorem ipsum dolor sit ame…onspectur cras amet。

如果要使用HTML帮助程序,请尝试此。假设您希望在第一个空格.IndexOf(“”)之前获取字符串的一部分(或者您可以像u所说的那样使用预定义的索引25):


ASP Net Core 3 MVC的HTML帮助程序

public static HtmlString Truncate(this IHtmlHelper helper, string text, int maxLength = 100)
{
    if (text == null) return new HtmlString("");

    if (text.Length > maxLength)
    {
        text = text.Substring(0, maxLength) + "...";
    }
    return new HtmlString($"{text}");
}
在cshtml文件中使用

@Html.Truncate(item.Description)
或者您可以使用param

@Html.Truncate(item.MusicUrl, 50)

如果字符串长度不超过25个字符,这将抛出一个
ArgumentOutOfRangeException
。@NathanA,这很好,但是你能提供选项2在结尾显示点,如
mydescription…..
,我试过了,解析时出错,你知道吗?@NathanA非常感谢你。我一直在努力解决这个问题,因为2个小时后终于找到了仍然不起作用的解决方案。DisplayFor只接受产生值(特别是错误中列出的值)的表达式,而不接受方法调用。这就是产生模板错误的原因。@NathanA你可能是对的。我没有检查对
DisplayFor
Display
的调用是什么。我认为命名的
显示
元素应该可以工作,但我只是猜测一下。这样看起来更好。良好的通用应用程序。您是否考虑过创建一个额外的模型属性来执行子字符串并使用它?是的,如果没有办法使用模板和单个属性,我就是这样做的。您这里的具体问题是传递到
Html.DisplayFor
的表达式必须引用实际属性,而不是特定值。换句话说,您只能执行
@Html.DisplayFor(m=>item.Description)
,而不能执行
@Html.DisplayFor(m=>item.Description.Substring(0,25))
。不过,您不需要为此使用
Html.DisplayFor
,因此您只需编写
@item.Description.Substring(0,25)
。但是,请记住@48klocs对Nathan A答案的评论。请注意,
Take()
返回一个
IEnumerable
,而不是字符串
Substring()
更适合于此。虽然这可能有效,但为什么要使用
DisplayFor
?以这种方式在数据模型的成员上使用它将失去任何好处。如果类型没有更改(字符串到字符串),则仍然可以使用相同的显示模板。看见
public static HtmlString Truncate(this IHtmlHelper helper, string text, int maxLength = 100)
{
    if (text == null) return new HtmlString("");

    if (text.Length > maxLength)
    {
        text = text.Substring(0, maxLength) + "...";
    }
    return new HtmlString($"{text}");
}
@Html.Truncate(item.Description)
@Html.Truncate(item.MusicUrl, 50)