自定义模板ValidationMessageFor [英] Custom ValidationMessageFor Template
问题描述
我使用ASP.NET MVC 3,现在用不显眼的jQuery客户端验证。
I'm using ASP.NET MVC 3 right now with unobtrusive jquery client validation.
在默认情况下,ValidationMessageFor产生某些类别的跨度标签和属性集。我想它生成一个不同的模板。例如,我可能要具有一定的背景图像div标签。
By default, ValidationMessageFor generates a span tag with certain classes and attributes set. I would like it to generate a different template instead. For example, I might want a div tag with a certain background image.
这是可能在所有的,或者我能不能从那里获得的纯文本错误信息,所以我可以做我自己的风格?
Is this possible at all, or can I just obtain the plain text error message from there so I can do my own styling?
感谢
推荐答案
在MVC 2中,跨度
很难codeD。有没有办法使用 DIV
不是与 ValidationMessageFor
。
In MVC 2, the span
is hard coded. There is no way to use a div
instead with ValidationMessageFor
.
您可以得到类似于内置的辅助是如何做的信息:
You can get the message similar to how the built-in helper does it:
if (modelError != null) {
builder.SetInnerText(GetUserErrorMessageOrDefault(htmlHelper.ViewContext.HttpContext, modelError, modelState));
}
private static string GetUserErrorMessageOrDefault(HttpContextBase httpContext, ModelError error, ModelState modelState) {
if (!String.IsNullOrEmpty(error.ErrorMessage)) {
return error.ErrorMessage;
}
if (modelState == null) {
return null;
}
string attemptedValue = (modelState.Value != null) ? modelState.Value.AttemptedValue : null;
return String.Format(CultureInfo.CurrentCulture, GetInvalidPropertyValueResource(httpContext), attemptedValue);
}
这篇关于自定义模板ValidationMessageFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!