asp.net-mvc - 在 DisplayName 属性中插入换行符

标签 asp.net-mvc asp.net-mvc-viewmodel

在我的 ViewModel 中,有一个属性需要 2 行标签,但是当我放置 <br /> 时在 DisplayName属性 HTML 代码将打印到页面而不是被解释为换行符。有没有办法获得 DisplayName其中有换行符吗?

查看:

    <tr>
        <td>
            @Html.LabelFor(m => m.GrossGallons)
        </td>
        <td>
        </td>
    </tr>

View 模型:

    [DisplayName("Gross Gallons <br /> (Max: 6,000)")]
    public decimal GrossGallons { get; set; }

尝试获取的输出:

Gross Gallons
(Max: 6,000)

最佳答案

有一个简单的方法可以做到这一点 - 使用 \n而不是<br /> ,并使用 CSS 使其工作。

型号:

[DisplayName("Gross Gallons\n(Max: 6,000)")]
public decimal GrossGallons { get; set; }

CSS:

label { white-space: pre-wrap; }

我建议使 CSS 选择器尽可能具体,以免捕获其他标签(如果您在其他地方手动使用标签,源代码可能包含空格)。例如,在 Bootstrap 中,我会将其应用于 label.control-label

您还可以仅将更具体的样式附加到该标签,并仅对该类设置样式。

@Html.LabelFor(m => m.GrossGallons, new { @class = "multiline-label" })

关于asp.net-mvc - 在 DisplayName 属性中插入换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22354573/

相关文章:

asp.net-mvc - 在没有任何 DI 库的情况下使用依赖注入(inject)

asp.net-mvc - 公共(public)静态类数据对于 ASp.net MVC 中的所有用户都相同吗?

asp.net-mvc - 如何在局部 View 中使用ViewModels

c# - 如何在我的 JSON 模型类中使用保留关键字作为标识符?

asp.net-mvc - ViewBag.Title 值覆盖 ASP.NET MVC 编辑器模板的 Model.Title

asp.net - 可重用的 jQuery 删除功能

c# - MVC - 将多个数据表传递给 View

c# - 是否可以重用 ViewModel 中的 DataAnnotations?

asp.net-mvc - 如何使我的自定义DropDownList编辑器模板的Required属性在客户端运行?

c# - 在 Razor MVC3 中使用 ViewModel 在单个 View 中显示多个模型( View 中只有详细信息)