在我的 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/