最大长度不起作用 Web API 2 模型
Maximum Length does not work Web API 2 Model
我有一个模型 class 来接收数据,我想将文本输入限制为 10 个字符,但是当我在我的测试方法中尝试它时,它接受的不止于此。这是我的代码:
public class Post
{
[Required]
public string userId { get; set; }
[Required, StringLength(1, MinimumLength = 10)]
public string dataText { get; set; }
[Required]
public int dataType { get; set; }
}
[TestMethod()]
public void AddPostTest()
{
SQLDB db = new SQLDB();
Post userpost = new Post();
userpost.userId = "1";
userpost.dataText = "fgfdgfdgfdgdfgfdgfdgfdgfdgfdgdfgfdg";
userpost.dataType = 1;
Assert.IsTrue(db.AddPost(userpost));
}
提前谢谢你。
StringLengthAttribute的语法如下:参考StringLengthAttribute MSDN
public StringLengthAttribute(
int maximumLength
)
[Required, StringLength(MaximumLength, MinimumLength)]
public string dataText { get; set; }
在您的情况下,您使用了 MaxLength 值 = 1 和 MinLength 值 = 10,这是不正确的。
请交换两个值
[Required, StringLength(10, MinimumLength = 1)]
public string dataText { get; set; }
或者只使用最大值
[Required, StringLength(10)]
public string dataText { get; set; }
我有一个模型 class 来接收数据,我想将文本输入限制为 10 个字符,但是当我在我的测试方法中尝试它时,它接受的不止于此。这是我的代码:
public class Post
{
[Required]
public string userId { get; set; }
[Required, StringLength(1, MinimumLength = 10)]
public string dataText { get; set; }
[Required]
public int dataType { get; set; }
}
[TestMethod()]
public void AddPostTest()
{
SQLDB db = new SQLDB();
Post userpost = new Post();
userpost.userId = "1";
userpost.dataText = "fgfdgfdgfdgdfgfdgfdgfdgfdgfdgdfgfdg";
userpost.dataType = 1;
Assert.IsTrue(db.AddPost(userpost));
}
提前谢谢你。
StringLengthAttribute的语法如下:参考StringLengthAttribute MSDN
public StringLengthAttribute(
int maximumLength
)
[Required, StringLength(MaximumLength, MinimumLength)]
public string dataText { get; set; }
在您的情况下,您使用了 MaxLength 值 = 1 和 MinLength 值 = 10,这是不正确的。 请交换两个值
[Required, StringLength(10, MinimumLength = 1)]
public string dataText { get; set; }
或者只使用最大值
[Required, StringLength(10)]
public string dataText { get; set; }