【发布时间】:2017-10-30 19:13:06
【问题描述】:
我目前正在使用 asp.net 核心构建博客。我知道我可以使用数据注释来限制标题中使用的字符串的长度。如何限制用户输入的标签数量?比如这里最多只能输入5个标签,在stackoverflow上。
public class Post
{
public int PostId { get; set; }
[MaximumLength(50)]
public string Title { get; set; }
public ICollection<PostTag> PostTags { get; set; }
}
public class PostTag
{
public int TagId { get; set; }
public Tag Tag { get; set; }
public int PostId { get; set; }
public Post Post { get; set;}
}
public class Tag
{
public int TagId { get; set; }
public string Text { get; set; }
public ICollection<PostTag> PostTags { get; set }
【问题讨论】:
-
你必须编写代码才能做到这一点,没有内置任何东西。
-
添加类似 Enumerable.TakeWhile
方法怎么样 (IEnumerable , Func ) -
您可以编写一个自定义验证器,用于提示查看here。如果您搜索一下,您还可以找到许多现有的问题。
标签: c# data-annotations