【发布时间】:2015-02-13 19:11:13
【问题描述】:
我在 VisualStudio 中使用来自模型的 Product 对象。我对自定义属性非常陌生。例如,我想将产品名称限制为最多 3 个单词。
[Required (ErrorMessage="Product name is required.")]
[Display(Name = "Product Name")]
[StringLength(30, ErrorMessage = "The {0} must be between {2} and {1} characters.", MinimumLength = 5)]
[ExcludeChar("/.,!@#$%", ErrorMessage = "Name contains invalid character.")]
// Custom annotation.
public object ProductName { get; set; }
【问题讨论】:
-
所以创建一个自定义属性并进行自定义验证。搜索“MVC 中的自定义验证”应该不会那么难。 here 就是一个例子
-
这里没有内置属性。可能是因为“三个词”的含义没有标准定义。您必须自己在自定义验证属性中定义逻辑。
标签: c# asp.net-mvc object custom-attributes