monkeySeeMonkeyDo
monkeySeeMonkeyDo

Reputation: 391

Rendering a display name containing HTML that's stored in Resource file

I need to render a HTML link that exists inside of a resource file

[Display(Name = "MyPropertyName" ResourceType = typeof(Res.Props))]
public bool MyProperty{ get; set; }

The MyPropertyName resource has some HTML:

"Click <a href="bla.com">here</a> to find out more."

I need to render this on the View so the link is clickable. The HTML will never render despite what method I attempt, some attempts (not exact syntax but you get the gist) will display the raw HTML, and some don't display anything at all.

var htmlLabel = Html.LabelFor(m => m."MyPropertyName" ).ToString()
Html.Raw(htmlLabel)

Html.Raw(Html.LabelFor(m => m."MyPropertyName" ).ToString())
Html.Raw(Html.LabelFor(m => m."MyPropertyName")

MvcHtmlString.Create(htmlLabel);
Html.Raw(MvcHtmlString.Create(htmlLabel))

etc.

I found a suggestion of getting the attribute info on the controller and passing it in via ViewBag and then rendering it with HTML.Raw, So I followed this here (and a a few others) but that code only pulls in the name of the property "MyPropertyName" and not string from the resource file.

Any ideas? Thanks.

Upvotes: 2

Views: 363

Answers (1)

monkeySeeMonkeyDo
monkeySeeMonkeyDo

Reputation: 391

well now this suddenly decided to work after one last silly attempt. For anyone that hits this kind of issue in the future:

The application has a StronglyTypedResourceBuilder class that contains all of the data I need. So the following works without trouble:

@using StringResources = Namespace.Resources.StringResources

...

@Html.CheckBoxFor(m => m.MyPropertyName)
@Html.Raw(@StringRes.MyPropertyName)

...

what a waste of a morning.

Upvotes: 2

Related Questions