Search code examples
c#asp.net-mvc-4label

Label not displaying converted decimal number


I have an ICollection of objects each containing a premium property of type decimal. In my view I have the following code where a table is populated with the premium properties

 @foreach (var section in Model.PolicySectionCollection)
 {
        <tr>
            <td>
                @Html.Label(section.Premium.ToString())
            </td>
        </tr>
 }

Inspecting

section.Premium.ToString()

using a quick view, reveals that the value is for example "12500.00"

Inspecting

@Html.Label(section.Premium.ToString())

using a quick view, reveals that the generated code is

<label for="">00</label>

So all my decimal numbers is displayed as 00. Is there any reason for this?


Solution

  • @Html.Label
    

    will produce a label for a provided property. In your case you're passing in a value as the property name. So it's treating "12500.00" as the name of a property you want to display. Of course that's not what you're intending to do, you simple want to display the value of a given property.

    Try

    @Html.Raw
    

    .. instead. All you want to do is print the actual value which @Html.Raw will do for you.