Search code examples
asp.net-mvcasp.net-core-mvcdisplayattribute

Is it possible to format Display attribute's Name property value of MVC Model class?


What I mean is, e.g.

[DataType(DataType.Text)]
[Display(Name = "Corporation / Entreprise")]
public string Corporation { get; set; }

Would it be possible to apply a line break after the '/' in the Name of the Display attribute? So when the View page displaying the label value which set in the Model:

<td class="col-md-6" style="font-weight:bold">
    @Html.DisplayNameFor(model => model.Corporation)
</td>

It would show something like:

Corporation / 
Entreprise

Instead of

Corporation / Entreprise

Thanks in advance.


Solution

  • Add the <br/> attribute in displayattribute and display it as Html.Raw in the view. like this:

    Model:

    [DataType(DataType.Text)]
    [Display(Name = "Corporation / <br/> Entreprise")]
    public string Corporation { get; set; }
    

    View:

    <td class="col-md-6" style="font-weight:bold">
          @Html.Raw(@Html.DisplayNameFor(model => model.Corporation))
    </td>
    

    Test Result:

    enter image description here