How do I prevent MVC3 html escaping my validation messages?
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. &开发者_开发知识库amp;lt;a href="%url_token%">Click here to reset.</a></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))
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.
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();
}
}
精彩评论