Claudio P Claudio P - 1 month ago 14
C# Question

Bold Text in Html.FormatValue using Razor

I want to have the following result. Username has to be bold:


Blabla Username Bla.


I have the Format in a ressource file:


Blabla {0} Bla.


And in the view I do the following:

@Html.FormatValue(User.Identity.Name, Resources.MyFormatString)


How can I make the Username bold and use Html.FormatValue? Or is there another method to achieve this?

Answer

You could simply change your resource to contain the bold-tag, strong-tag or a style.

Like "Blabla <b>{0}</b> Bla.".

[edit]
Indeed, checked Html.FormatValue for an escape functionality, did not see one, but apparently it does :)

In that case using @Html.Raw and string.Format will work.

@Html.Raw(string.Format(Resources.MyFormatString, "SomeName"))

(tested in MVC 5, but @Html.Raw is also available in 4)

Also a small note: storing HTML in resources is probably not the best idea, mixing UI & content.
[/edit]

Comments