Dai*_*ges 2 c# validation asp.net-web-api2
我有一个模型类来接收数据,我想将文本输入限制为 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));
}
Run Code Online (Sandbox Code Playgroud)
先感谢您。
StringLengthAttribute的语法如下:参考StringLengthAttribute MSDN
public StringLengthAttribute(
int maximumLength
)
[Required, StringLength(MaximumLength, MinimumLength)]
public string dataText { get; set; }
Run Code Online (Sandbox Code Playgroud)
在您的情况下,您使用的 MaxLength 值 = 1 和 MinLength 值 = 10 这是不正确的。请交换两个值
[Required, StringLength(10, MinimumLength = 1)]
public string dataText { get; set; }
Run Code Online (Sandbox Code Playgroud)
或者只使用最大值
[Required, StringLength(10)]
public string dataText { get; set; }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3472 次 |
| 最近记录: |