您将需要编写一个自定义助手来实现这一点。内置ValidationMessageFor
帮助器自动对值进行 HTML 编码。这是一个例子:
public static class ValidationMessageExtensions
{
public static IHtmlString MyValidationMessageFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> ex
)
{
var htmlAttributes = new RouteValueDictionary();
string validationMessage = null;
var expression = ExpressionHelper.GetExpressionText(ex);
var modelName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expression);
var formContext = htmlHelper.ViewContext.ClientValidationEnabled ? htmlHelper.ViewContext.FormContext : null;
if (!htmlHelper.ViewData.ModelState.ContainsKey(modelName) && formContext == null)
{
return null;
}
var modelState = htmlHelper.ViewData.ModelState[modelName];
var modelErrors = (modelState == null) ? null : modelState.Errors;
var modelError = (((modelErrors == null) || (modelErrors.Count == 0))
? null
: modelErrors.FirstOrDefault(m => !String.IsNullOrEmpty(m.ErrorMessage)) ?? modelErrors[0]);
if (modelError == null && formContext == null)
{
return null;
}
var builder = new TagBuilder("span");
builder.MergeAttributes(htmlAttributes);
builder.AddCssClass((modelError != null) ? HtmlHelper.ValidationMessageCssClassName : HtmlHelper.ValidationMessageValidCssClassName);
if (!String.IsNullOrEmpty(validationMessage))
{
builder.InnerHtml = validationMessage;
}
else if (modelError != null)
{
builder.InnerHtml = GetUserErrorMessageOrDefault(htmlHelper.ViewContext.HttpContext, modelError, modelState);
}
if (formContext != null)
{
bool replaceValidationMessageContents = String.IsNullOrEmpty(validationMessage);
builder.MergeAttribute("data-valmsg-for", modelName);
builder.MergeAttribute("data-valmsg-replace", replaceValidationMessageContents.ToString().ToLowerInvariant());
}
return new HtmlString(builder.ToString(TagRenderMode.Normal));
}
private static string GetUserErrorMessageOrDefault(HttpContextBase httpContext, ModelError error, ModelState modelState)
{
if (!String.IsNullOrEmpty(error.ErrorMessage))
{
return error.ErrorMessage;
}
if (modelState == null)
{
return null;
}
var attemptedValue = (modelState.Value != null) ? modelState.Value.AttemptedValue : null;
return string.Format(CultureInfo.CurrentCulture, "Value '{0}' not valid for property", attemptedValue);
}
}
进而:
public class MyViewModel
{
[Required(ErrorMessage = "Error Line1<br/>Error Line2")]
public string SomeProperty { get; set; }
}
在视图中:
@model MyViewModel
@using (Html.BeginForm())
{
@Html.EditorFor(x => x.SomeProperty)
@Html.MyValidationMessageFor(x => x.SomeProperty)
<button type="submit">OK</button>
}
如果您想在 ValidationSummary 中显示错误消息,您还可以编写一个自定义帮助程序,它不会对错误消息进行 HTML 编码,如this post
.