使用ASP.NET MVC 3定位非数据注释错误的最佳方法是什么? [英] What's the best way to localize non Data Annotation Errors with ASP.NET MVC 3?
问题描述
借助数据注释,现在可以很容易地使用Resource.resx文件来定位错误消息,例如:
With Data Annotations it's now easy to localize error messages using Resource.resx files like this for example:
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 a Student has already made a Payment for a given 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'm doing the following in my Services layer:
if (CheckIfAlreadyPaid(payment))
{
modelState.AddModelError("AlreadyPaid",
Resources.Views.Payment.PaymentCreateResources.AlreadyPaid);
}
它可以工作,但是我对在服务层中引用资源文件不抱有信心.
It works, but I don't fell confident about referencing the Resource file inside the Services layer.
是否有一种标准或更好的方法来定位与模型属性(数据注释)无关的错误消息-来自业务逻辑规则的错误?我还应该将这些错误添加到ModelStateDictionary吗?
Is there a standard or better way of localizing error messages that are not tied to model properties (Data Annotation) - errors that come from business logic rules? Should I still add these errors to the ModelStateDictionary?
推荐答案
我以不同的方式做到了. Service
层用于检查是否已经付款.在我的Controller
中,我向ModelState对象添加了一条验证错误消息,并向其传递了本地化的字符串资源.现在,我对这种方法感到更加自在.
I did it in a different way. The Service
layer is used to check if the Payment was already made. In my Controller
I add a validation error message to the ModelState object passing to it a localized string resource. Now I feel more comfortable with this approach.
这是代码:
/// <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;
}
为了补充我的答案,我今天才发现 ValidationSummary @Html.ValidationSummary(true)
完全符合我的要求:
To complement my answer, I just found today that ValidationSummary @Html.ValidationSummary(true)
does exactly what I want:
Html.ValidationSummary
返回无序列表(ul元素)ModelStateDictionary
对象中的验证消息的数量,并且可以选择仅显示模型模型级别的错误.
Html.ValidationSummary
returns an unordered list (ul element) of validation messages that are in theModelStateDictionary
object and optionally displays only model model-level errors.
我通过了true
,它将仅在页面顶部的摘要中显示模型级别的错误(非数据注释错误).这很棒,但前提是它可以工作...:)
I'm passing true
and it will only display model-level errors (non data annotation errors) in the summary just at the top of the page. This is great but only if it would work... :)
我遇到一个问题,当我设置ValidationSummary(true)
时,没有出现与模型属性无关的自定义错误消息.然后,我使用Google进行搜索,发现此链接在Google中书籍(史蒂文·桑德森撰写的Pro ASP.NET MVC 2框架).
I was facing a problem where my custom error messages not tied to model properties weren't appearing when I set ValidationSummary(true)
. I then searched using Google and found this post. I tried his solution but it didn't work. I then searched a little bit more and found this link in Google Books (Pro ASP.NET MVC 2 Framework by Steven Sanderson).
我尝试了那里描述的内容,并传递了一个空字符串作为键(string.Empty
),它完成了工作.
I tried what is described there passing an empty string as key (string.Empty
) and it did the job.
if(paymentService.IsPaymentMade(payment))
{
ModelState.AddModelError(string.Empty, Localization.PaymentAlreadyCreated);
}
这篇关于使用ASP.NET MVC 3定位非数据注释错误的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!