Reputation:
I am binding a model to view.
public class Validation
{
[Display("Please enter in <h5>day(s)</h5>")]
public string Message { get; set; }
}
Here Message
property will be set as Please enter in <h5>day(s)</h5>
.
In view am binding model property to LabelFor
.
@Html.LabelFor(m => m.Message)
Expecting output as
Please enter in day(s)
But actual output is
Please enter in <h5>day(s)</h5>
I want part of the string to vary in size and cosmetics, so applying CSS to the entire label is not what I'm looking for.
Upvotes: 0
Views: 1469
Reputation: 151720
Your display string "<h5>some text</h5>"
will be rendered as text, not HTML. MVC will encode the <
>
characters to <
>
, causing them to be displayed as-is.
You shouldn't want to do this like this anyway.
The proper way would be to apply the display string to contain the text you want:
[Display("some text")]
Then create a CSS class:
label.fancyLabel {
/* your style here that makes a label look like a h5 */
}
Then apply that class to the label (from .NET MVC - How to assign a class to Html.LabelFor?):
@Html.LabelFor(m => m.Message, new { @class= "fancyLabel" })
As for your edit, if you want to just render the Display
attribute's text as literal HTML, you'll have to:
for=
attribute).@Html.Raw()
that value as the label text.@Html.YourLabelHelper(m => m.Message)
to render it.But using HTML on your models like that isn't really advisable. Views and templates should decide how to render a model, not the other way around.
Upvotes: 1