(Razor) Html.Helper 中的字符串长度?

发布于 2024-12-03 03:55:19 字数 257 浏览 1 评论 0原文

这是一个非常简单的问题。

我有一个 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

初吻给了烟 2024-12-10 03:55:19

可以考虑 3 种可能性:

  1. 在将映射层中的文本发送到视图之前(将域模型转换为视图模型时)
  2. 将其剥离 编写自定义 HTML 帮助程序
  3. 为给定类型编写自定义显示模板,然后 3指示正确显示模板的可能性: 1) 依赖约定(在这种情况下无需执行任何操作,将自动选择模板) 2) 使用 UIHint 属性装饰视图模型属性 3) 将模板名称作为第二个参数传递给显示助手。

There are 3 possibilities that could be considered:

  1. Strip the text in your mapping layer before sending it to the view (when converting your domain model to a view model)
  2. Write a custom HTML helper
  3. Write a custom display template for the given type and then 3 possibilities to indicate the correct display template: 1) rely on conventions (nothing to do in this case, the template will be automatically picked) 2) decorate your view model property with the UIHint attribute 3) pass the template name as second argument to the DisplayFor helper.
御弟哥哥 2024-12-10 03:55:19

我需要同样的东西并用以下几行解决了这个问题。

<td>
    @{
        string Explanation = item.Explanation;
        if (Explanation.Length > 10) 
        {  
            Explanation = Explanation.Substring(0, 10);
        }
    }
@Explanation
</td>

如果你的字符串总是大于10,你可以排除:

if (Explanation.Length > 10) 
{
    Explanation = Explanation.Substring(0, 10);
}

并直接写:

string Explanation = item.Explanation.Substring(0, 10);

另外我建议为大于你给出的限制的字符串添加 ..

I needed the same thing and solved the case with the following lines.

<td>
    @{
        string Explanation = item.Explanation;
        if (Explanation.Length > 10) 
        {  
            Explanation = Explanation.Substring(0, 10);
        }
    }
@Explanation
</td>

If your string is always larger than 10, you can rule out:

if (Explanation.Length > 10) 
{
    Explanation = Explanation.Substring(0, 10);
}

And directly write:

string Explanation = item.Explanation.Substring(0, 10);

Also I recommend adding .. for strings larger than the limit you give.

倦话 2024-12-10 03:55:19

您可以只在视图模型中添加一个属性来截断字符串并显示它:

// View model
public string TextShort { get { return Text.Substring(0, 10); } }

// View
@Html.DisplayFor(modelItem => item.TextShort)

You could just add a property onto your view model that does the truncation of the string and display that instead:

// View model
public string TextShort { get { return Text.Substring(0, 10); } }

// View
@Html.DisplayFor(modelItem => item.TextShort)
是你 2024-12-10 03:55:19

更改

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

@Html.Display(item.Text.Length > 10 ? item.Text.Substring(0,10) : item.Text)

Change

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

to

@Html.Display(item.Text.Length > 10 ? item.Text.Substring(0,10) : item.Text)
故事和酒 2024-12-10 03:55:19

编辑:新答案

关于

@{
 modelItem.ShortText= model.Text.Substring(0, ....);
}

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

DisplayFor 的原型是:

public static MvcHtmlString DisplayFor<TModel, TValue>(
    this HtmlHelper<TModel> html,
    Expression<Func<TModel, TValue>> expression
)

我认为 modelItem 是动态的,因此应该可以向视图模型添加新属性。

Edited : New Answer

what about

@{
 modelItem.ShortText= model.Text.Substring(0, ....);
}

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

The prototype for DisplayFor is :

public static MvcHtmlString DisplayFor<TModel, TValue>(
    this HtmlHelper<TModel> html,
    Expression<Func<TModel, TValue>> expression
)

And the modelItem is a dynamic I think, so it should be possible to add anew property to the view model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文