I've found numerous examples of how to use MarkupString and it's just not doing what it's supposed to.
In my blazor component I have:
<small @ref="HelpText">
@(!string.IsNullOrWhiteSpace(Model.HelpText) ? (MarkupString)Model.HelpText : "Double-click here to add text")
</small>
I have a simple model right now where I initialize HelpText to
"<b><i>This is a test</b></i>"
In my webpage I get that exact output displayed. (See attached picture).
CodePudding user response:
This is actually do to the vagaries of ?:
. Somehow the MarkupString value is converted to object?
and then to string
again.
To be honest I was surprised your code compiles, the two branches of ?:
must be 'assignment compatible', and MarkupString cannot be assigned to string.
You can do this:
<small @ref="HelpText">
@((MarkupString)(!string.IsNullOrWhiteSpace(ModelHelpText) ? ModelHelpText : "Double-click here to add text"))
</small>
but that is getting so ugly I would move it to the @code section.