Search code examples
c#asp.net-mvcrazorhtml-helper

LabelFor not displaying value?


I'm trying to just display a value in my Razor code. I've tried both DisplayFor and LabelFor. If I use DisplayFor then my value shows up but my CSS doesn't get applied. If I use LabelFor then it's the opposite, my CSS is applied but the text only displays as "EmployeeId". I've confirmed that my ViewModel has a value populated for the EmployeeId prior to going to View.

This is what I've tried:

<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>

Solution

  • first of all DisplayFor does not have an overload of htmlAttributes so new { @class = "control-label label-value" } wont work and secondly LabelFor does not display value of the property it will just display the name of the property so you can do it like this

    <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>