5

I'm trying to output a validation message in MVC3 which contains a link.

I'm outputting the error message place holder like this

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

The problem is, the error message is html escaped which is fine most times, but I'd like a link to be in the middle.

<span class="field-validation-error" data-valmsg-for="Email" data-valmsg-replace="true">This e-mail address is already registed. &lt;a href=&quot;%url_token%&quot;&gt;Click here to reset.&lt;/a&gt;</span>

How do I prevent this from happening?

This works but is not a solution, rather, a temporary work around.

@{
  string s = Html.ValidationMessageFor(model => model.Email).ToString();
}
@Html.Raw(HttpUtility.HtmlDecode(s))
Razor
  • 17,271
  • 25
  • 91
  • 138

2 Answers2

1

Looking at the code with reflector it doesn't seem to have a method for that, the more complete overload is:

public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, TProperty>> expression,
    string validationMessage,
    object htmlAttributes)
{
    return htmlHelper.ValidationMessageFor<TModel, TProperty>(expression, validationMessage, ((IDictionary<string, object>) HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
}

What you can do however is to create an extension method that returns the string as you want, like this:

public static class ValidationExtender
{
    public static IHtmlString HtmlValidationMessageFor<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, TProperty>> expression)
    {
        return MvcHtmlString.Create(htmlHelper.ValidationMessageFor(expression).ToHtmlString());
    }
}

You can use a MvcHtmlString. See MvcHtmlString.Create(). It will output the html without escaping.

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • I can't actually get this solution to work. The MvcHtmlString class' sole purpose is to escape HTML code. From the documentation "Represents an HTML-encoded string that should not be encoded again." – Razor Jun 27 '11 at 09:40
0

Taking @BrunoLM's lead, The following extension methods should give you what you need. I've only done basic testing on this, but it does work.

public static class HtmlHelperExtensions
{
    private static readonly string htmlErrorPlaceholder = "##__html#Error#Placeholder__##";

    public static IHtmlString HtmlValidationMessageFor<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, TProperty>> expression,
        object htmlAttributes)
        {
            var name      = expression.GetMemberName();
            var isInError = htmlHelper.ViewContext.ViewData.ModelState.ContainsKey(name);
            var message   = htmlHelper.ValidationMessageFor(expression, htmlErrorPlaceholder, htmlAttributes);
            if (isInError && !MvcHtmlString.IsNullOrEmpty(message))
            {
                var realError = htmlHelper.ViewContext.ViewData.ModelState[name].Errors.First().ErrorMessage;
                return htmlHelper.Raw(message.ToString().Replace(htmlErrorPlaceholder, realError));
            }

            return MvcHtmlString.Empty;
        }
}

public static class Expression_1Extensions
{
    public static string GetMemberName<TModel, TProperty>(this Expression<Func<TModel, TProperty>> expression)
    {
        switch (expression.Body.NodeType)
        {
            case ExpressionType.MemberAccess:
                MemberExpression memberExpression = (MemberExpression)expression.Body;
                return memberExpression.Member is PropertyInfo ? memberExpression.Member.Name : null;
        }

        throw new NotSupportedException();
    }
}
Greg B
  • 14,597
  • 18
  • 87
  • 141