开发者

ASP MVC 2 Custom CLASS Validation Attribute error messages not displaying

开发者 https://www.devze.com 2023-01-06 03:12 出处:网络
In my model metadata class, I\'m using the data annotation attributes for poperty validation.This works fine.

In my model metadata class, I'm using the data annotation attributes for poperty validation. This works fine.

I have created an Attributes namespace and 4 custom validation attributes deriving from System.DataAnnotations.ValidationAttribute in it; all these attributes validate the entire model itself, because they have to compare multiple fields. I used the PropertiesMustMatch attribute as an example. I know these attributes work, because I have tested them extensively. The problem is when one of these attributes fires an error, it just re-renders the form, and once you fix the problem, you can safely submit and it works. But no error messages ever get displayed from the attributes.

My question is how can I wire up the error messages to ValidationSummary() ?

I'm using Html.ValidationSummary(true) in the views.

I can post code if it's necessary, but it would break horribly if I copied and pasted into this box since it's only about 80 characters in width and I let error message strings go off the screen most of the tim开发者_如何学编程e.


Are you overriding the FormatErrorMessage() method in your class?

public override string FormatErrorMessage(string name)
{
   return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
                        OriginalProperty, ConfirmProperty);
}

FYI, paste code in with a leading four space buffer on the left and it will be formatted just fine (at least sufficient for our purposes).


I saw this question because I have the same problem, and the solution of Andrew is good but does not solve the problem of user385060. Because the problem is not the space, is the execution stack, if you use a dataanotations validations, the validations of fields execute first and if no errors are found, execute validation of class level. I hope this answer help some others people.

0

精彩评论

暂无评论...
验证码 换一张
取 消