c# - LabelFor不显示值?

标签 c# asp.net-mvc razor html-helper

我试图在 Razor 代码中显示一个值。我尝试过 DisplayFor 和 LabelFor。如果我使用 DisplayFor 那么我的值会显示,但我的 CSS 不会被应用。如果我使用 LabelFor 则相反,我的 CSS 已应用,但文本仅显示为“EmployeeId”。在转到 View 之前,我已确认我的 ViewModel 已为 EmployeeId 填充了值。

这是我尝试过的:

<div class="row voffset2">
    <div class="col-md-12">
        Employee ID: @Html.LabelFor(m => m.EmployeeId, new { @class = "control-label label-value" })
    </div>
</div>

<div class="row voffset2">
    <div class="col-md-12">
        Employee ID: @Html.DisplayFor(m => m.EmployeeId, new { @class = "control-label label-value" })
    </div>
</div>

最佳答案

首先DisplayFor没有 htmlAttributes 的重载,因此 new { @class = "control-label label-value"} 不起作用,其次 LabelFor不显示属性的值,它只会显示属性的名称,因此 你可以这样做

<div class="row voffset2">
    <div class="col-md-12">
        Employee ID: <span class"ontrol-label label-value">@Html.DisplayFor(m => m.EmployeeId)</span>
    </div>
</div>

关于c# - LabelFor不显示值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43639977/

相关文章:

C# - 创建 EnumRandomizer 类

c# - 为什么我的 View 不使用 EditorTemplates 文件夹来执行创建操作?

c# - 为什么这个任务会提前返回?我做错了什么吗?

C# - 将多个值添加到 MySQL 中的参数

jquery - K-state-专注于剑道窗口,屏幕

c# - 使 Azure blob URL 仅可供登录用户访问

c# - ASP.NET MVC 网络 API : application/xml PUT request body is null on server side

asp.net-mvc - 如何使用 RazorEngine 从模板发送电子邮件

html - 在 MVC razor 中显示来自模型的列表

c# - 在 C# 中的 nestedProperty 上使用 InvokeMember