For some reason the otherwise delightful nuget Package FoolProof Validation does not work with modern version of the entity framework – rather, the validation works just fine, but it chokes when you try to update the database with entity framework.
The workaround I found was to validate on a view model – and then translate that viewmodel to the actual model, which is then sent to the database.
|
Written By Steve French |
One response to “How to fix problems with FoolProof Validation in asp.net mvc”
Leave a Reply
Hi Steve!
I found another solution about this problem
For anyone looking for a solution to this for .Net 4. Grab a copy of the source code from http://foolproof.codeplex.com/ and update the target platform to .Net 4 and add the following method to the ModelAwareValidationAttribute class.
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var container = validationContext.ObjectInstance;
#region This block of code decompiled from namespace System.ComponentModel.DataAnnotations.ValidationAttribute
ValidationResult local_0 = ValidationResult.Success;
if (!this.IsValid(value, container)) // Change to decompiled code here to call our abstract implementation instead of the NotImplemented IsValid(object value) method above
{
string[] temp_26;
if (validationContext.MemberName == null)
temp_26 = (string[])null;
else
temp_26 = new string[1]
{
validationContext.MemberName
};
string[] local_1 = temp_26;
local_0 = new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName), (IEnumerable)local_1);
}
return local_0;
#endregion
}
the link of solution: http://forums.asp.net/t/1752975.aspx?Conditional+validation+with+MVC+Foolproof+Validation+
Thanks!