【发布时间】:2015-01-07 19:54:19
【问题描述】:
我在模型中有 ASP.NET 网站属性:
[Required]
[Display(Name = "bla bla")]
[MinMaxLength(50,2000)]
public string TextDetail { get; set; }
我已经本地化了
public class MinMaxLengthAttribute : StringLengthAttribute
{
public MinMaxLengthAttribute(int minimum, int maximum)
:base(maximum)
{
ErrorMessage = "{0} must contain from {2} to {1} characters."; //this text is localized, that is why I need to use special class
MinimumLength = minimum;
}
}
当我查看日志时,我可以找到
Exception (level 0):
Message = bla bla must contain from 50 to 2000 characters.
如果用户写的字符串太短或太长,我想记录。有没有办法在 StringLengthAttribute 中记录字符串的当前长度?
【问题讨论】:
-
为什么要创建 MinMaxLengthAttribute?为什么不使用 StringLengthAttribute?
-
因为 StringLengthAttribute 仅适用于英文网站,请参阅评论。据我所知,我必须对自定义本地化消息进行此类自定义。如果有简单的解决方案,请告诉我。
-
我们一直使用资源文件来获取本地化错误消息。为什么StringLengthAttribute的错误信息也不能做到这一点?
标签: asp.net-mvc validation data-annotations