2016-07-25 63 views
0

我利用模型验证功能是这样的:依赖于ASP.NET核心应用使用模型验证

[StringLength(60, MinimumLength = 3)] 
[Required] 

但是当我启动应用程序,我得到这样的:

Author.cs(8,6): error CS0246: The type or namespace name 'StringLengthAttribute' could not be found (are you missing a using directive or an assembly reference?) 
Author.cs(8,6): error CS0246: The type or namespace name 'StringLength' could not be found (are you missing a using directive or an assembly reference?) 
Author.cs(9,6): error CS0246: The type or namespace name 'RequiredAttribute' could not be found (are you missing a using directive or an assembly reference?) 
Author.cs(9,6): error CS0246: The type or namespace name 'Required' could not be found (are you missing a using directive or an assembly reference?) 
Author.cs(8,23): error CS0246: The type or namespace name 'MinimumLength' could not be found (are you missing a using directive or an assembly reference?) 
Author.cs(11,6): error CS0246: The type or namespace name 'StringLengthAttribute' could not be found (are you missing a using directive or an assembly reference?) 
Author.cs(11,6): error CS0246: The type or namespace name 'StringLength' could not be found (are you missing a using directive or an assembly reference?) 
Author.cs(12,6): error CS0246: The type or namespace name 'RequiredAttribute' could not be found (are you missing a using directive or an assembly reference?) 
Author.cs(12,6): error CS0246: The type or namespace name 'Required' could not be found (are you missing a using directive or an assembly reference?) 
Author.cs(11,23): error CS0246: The type or namespace name 'MinimumLength' could not be found (are you missing a using directive or an assembly reference?) 

我找不到我丢失的包。我的猜测是Microsoft.AspNetCore.Mvc.DataAnnotations,但似乎并非如此。

那么我需要什么依赖?

+0

似乎是正确的,你有没有进口正确的命名空间?应该是'使用System.ComponentModel.DataAnnotations'或者'使用System.ComponentModel.Annotations'。只要在错误上点击'Ctrl' +'.'来让VS建议你一个命名空间 – Tseng

+0

是的,我忘了'使用'!非常感谢 – vanthome

+0

我发表了评论作为答案 – Tseng

回答

1

依赖关系似乎是正确的。

但是只导入库/ nuget包并不意味着你可以使用它。你也需要把using System.ComponentModel.DataAnnotations(或using System.ComponentModel.Annotations你* cs文件的顶部。

或者只是打Ctrl + .上的错误,让VS建议你正确的命名空间。