Reputation: 29
I'm using ASP.NET MVC 3 right now with unobtrusive jquery client validation.
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?
Thanks
Upvotes: 1
Views: 2366
Reputation: 126577
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);
}
Upvotes: 0