asp.net-mvc-3razorresourcesmvchtmlstring

HtmlTags within Resx displayed in @Html.ValidationMessageFor


I have the following doubt. I am considering the option to have html tags within my resx texts for localization. When I put the resources directly I can resort to:

@Html.Raw(@Resources.ResourcesFM.Error_Email)

and it works as expected. The problem is when the resource is being called by a validation message from an htmlhelper:

@Html.ValidationMessageFor(model => model.Email)

Got from an attibute:

[DataType(DataType.EmailAddress,
ErrorMessageResourceType = typeof(ResourcesFM),
ErrorMessageResourceName = "ErrorMailIncorr")]

What I am trying...

@Html.Raw(Html.ValidationMessageFor(model => model.Email))

I do not know how to get the same result as when using @html.Raw as the output from the helper is a MvcHtmlString...

Thanks


Solution

  • Try this:

    View:

    @Html.Raw(Server.HtmlDecode(@Html.ValidationMessageFor(m => m.UserName).ToString()))
    

    Controller Action:

    ModelState.AddModelError("UserName", "This is a link <a href='http://example.com'>Google Home</a>");