2

In my scenario I'd like to display a text with URL link within validation message attached to form element on MVC view. I'm using ValidationExtensions.ValidationMessage extension method, like this:

<%=Html.ValidationMessage(Model.Name) %>

The behavior I'm seeing is that validation message is HTML escaped, what effectively prevents me from adding a link to message. Is there a way to circumvent this behavior? My error messages aren't user-supplied, so I don't think I have to worry about output sanitization here...

2 Answers 2

3

I'm guessing that since Html.ValidationMessage is built in you're going to either create your own version, or if you're feeling creative, since it returns a string, assign that and then unescape the characters you want to change back.

string validation = Html.ValidationMessage(Model.Name);
validation = Regex.Replace(validation, "&gt;", "<");
//etc...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I've created an 'UnEscape()' extension method for string - and now I can use it whenever I need.
3

You could use the HttpUtility.HtmlDecode(...) method along with your Html.ValidationMessage(...) method to get 'err done :D

1 Comment

@Html.Raw(HttpUtility.HtmlDecode(Html.ValidationMessageFor(x =>x.Name).ToHtmlString()));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.