What is the best way to localize data annotation errors without ASP.NET MVC 3?

With data annotations, it is now easy to localize error messages using Resource.resx files, for example, such as:

public class Student
{
    . . .

    [Required(ErrorMessageResourceName ="Required",
     ErrorMessageResourceType = typeof(StudentResources))]
    [StringLength(16)] 
    [Display(Name = "FirstName", ResourceType = typeof(StudentResources))]
    public string FirstName { get; set; }

    . . .
}

Now, let's say I want to check if the Student has already paid for this month and year:

public bool CheckIfAlreadyPaid(Payment payment)
{
    return repository.GetPayments().Any(p => p.StudentId == payment.StudentId &&
                                        p.Month == payment.Month &&
                                        p.Year == payment.Year);
}

If he has already made the payment, I do the following at my service level:

if (CheckIfAlreadyPaid(payment))
{
    modelState.AddModelError("AlreadyPaid",
    Resources.Views.Payment.PaymentCreateResources.AlreadyPaid);
}

This works, but I'm not sure referring to a service level resource file.

Is there a standard or better way to localize error messages that are not tied to model properties (Data Annotation) - errors that arise from business logic rules? Should I add these errors to ModelStateDictionary?

+5
2

-. Service , . Controller ModelState, . .

:

/// <summary>
/// Performs validation of business logic...
/// </summary>
/// <param name="payment"></param>
/// <returns></returns>
private bool ValidatePayment(Payment payment)
{
    if (paymentService.IsPaymentMade(payment))
    {
        ModelState.AddModelError("AlreadyPaid", Localization.AlreadyPaid);
    }

    return ModelState.IsValid;
}

EDIT:

, , ValidationSummary @Html.ValidationSummary(true) , :

Html.ValidationSummary ( ul) , ModelStateDictionary , , .

true, ( ) . , ...:)

, , , , ValidationSummary(true). Google . , . Google (Pro ASP.NET MVC 2 Framework ).

, , (string.Empty), .

if(paymentService.IsPaymentMade(payment))
{
    ModelState.AddModelError(string.Empty, Localization.PaymentAlreadyCreated);
}
0

, , . , - ( Fluent Validation ). , , , MVC, .

0

All Articles