【问题标题】:Need to truncate Razor HTML DisplayFor Helper需要截断 Razor HTML DisplayFor Helper
【发布时间】:2014-02-18 16:29:50
【问题描述】:

我正在尝试从数据库中截断有时非常大或有时为空的文本字段,即

@Html.DisplayFor(modelItem => item.MainBiography)

最后用三个点替换。

我已经尝试了 substring 函数,但一直出错。

任何指点,谢谢!

更新:

... 不是很重要,所以我尝试使用

 @Html.DisplayFor(modelItem => item.MainBiography.Substring(0,10)) 

并得到以下错误代码:

System.InvalidOperationException 未被用户代码处理 HResult=-2146233079 Message=模板只能用于字段访问、属性访问、单维数组索引或单参数自定义索引器表达式。 Source=System.Web.Mvc –

【问题讨论】:

  • 向我们展示代码...您遇到了什么错误?
  • System.InvalidOperationException 未被用户代码处理 HResult=-2146233079 Message=模板只能用于字段访问、属性访问、单维数组索引或单参数自定义索引器表达式。 Source=System.Web.Mvc

标签: c# asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

您最好在模型中创建一个不同的属性,以获取 MainBiography 并最终将其缩短。

像这样:

// This is your property (sampled)
public string MainBiography { get; set; }

//How long do you want your property to be at most ?
//(Best practice advices against magic numbers)
private int MainBiographyLimit = 100;

//You probably need this if you want to use .LabelFor() 
//and let this property mimic the "full" one  
[Display(Name="Main Biography")]
public string MainBiographyTrimmed
{
    get
    {
        if (this.MainBiography.Length > this.MainBiographyLimit)
            return this.MainBiography.Substring(0, this.MainBiographyLimit) + "...";
        else 
            return this.MainBiography;
    }
}

用法是

@Html.DisplayFor(item => item.MainBiographyTrimmed)

另一种方法是构建一个成熟的视图模型,但我发现它往往过于矫枉过正。

【讨论】:

  • 我在 Helper 中遇到错误 - 无法在此范围内声明名为“item”的局部变量,因为它会给“item”赋予不同的含义,它已在“父级或current' 范围来表示其他东西所以我使用了 @Html.DisplayFor(modelItem => item.SpecialistInterestsTrimmed) 和它的罚款。
  • @Html.DisplayFor(modelItem => item.MainBiographyTrimmed)
  • @Alex 这也可以返回 string.Format("{0}{1}", this.MainBiography.Substring(0, this.MainBiographyLimit), HttpUtility.HtmlDecode("…")) 以获得适当的省略号。
【解决方案2】:

DisplayFor 需要一个表示属性的表达式。

您可以向模型添加属性:

public string MainBiographyTrimmed
{
    get
    {
        if (MainBiography.Length > 10 ) { return MainBiography.Substring(0, 10) + "...";}
        else { return MainBiography; }

    }
}

那么在你看来,就这样做:

@Html.DisplayFor(item => item.MainBiographyTrimmed)

【讨论】:

    【解决方案3】:

    您可以编写一个字符串扩展方法并在您的模型属性(字符串类型)上调用它。您可以通过子字符串操作的限制。

    public static class StringExtensions
    {
        public static string SubStringTo(this string thatString, int limit)
        {
            if (!String.IsNullOrEmpty(thatString))
            {
                if (thatString.Length > limit)
                {
                    return thatString.Substring(0, limit);
                }
                return thatString;
            }
            return string.empty;
        }
    }
    

    在您的视图中,导入您拥有扩展方法的命名空间并在您的字符串属性上调用该方法。

    @Html.DisplayFor(modelItem => item.MainBiography.SubStringTo(10)) 
    

    【讨论】:

      【解决方案4】:

      尝试使用Html.Display 而不是DisplayFor,如下所示:

       @Html.Display(item.MainBiography.Substring(0, 10), null, "MainBiography")
      

      顺便说一句,你的两个 lambda 语句都是无效的。参数名称应该相同。

      不是这样的:

      @Html.DisplayFor(modelItem => item.MainBiography)
      

      应该是:

      @Html.DisplayFor(modelItem => modelItem.MainBiography)
      

      或者:

      @Html.DisplayFor(item => item.MainBiography)
      

      【讨论】:

      • 如果这是嵌套类型,他将丢失使用Display的html字段名称(前缀)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      • 2013-04-05
      • 2014-07-24
      • 1970-01-01
      • 2016-06-22
      相关资源
      最近更新 更多