我想在每个验证错误消息前面添加一个图标。 我正在尝试这个:
public static IHtmlString BogusValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expr)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expr, htmlHelper.ViewData);
MvcHtmlString validationError = htmlHelper.ValidationMessageFor<TModel, TProperty>(expr);
var imgErr = new TagBuilder("img");
imgErr.Attributes.Add("src", "../../Content/images/error.png");
return new HtmlString(imgErr.ToString() + validationError.ToString());
}
我明白了:
<label for="Email">Email address</label>
<input id="Email" type="text" value="" name="Email" data-val-required="The Email address field is required." data-val="true">
<img src="../../Content/images/error.png">
<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="Email"></span
我从头开始看到错误图标。 我怎么能把那个img放在跨度里?我如何访问其InnerHtml属性?
答案 0 :(得分:1)
我认为您可以更轻松地使用css将背景图像应用于field-validation-error
的跨度。
.field-validation-error {
background:url(../../Content/images/error.png) no-repeat left center;
padding-left:20px; /* or whatever you image size is */
}