1

我在 MVC 4 中遇到上述包的问题。即使按下按钮发布数据后验证也不会触发。唯一触发的是来自 DataAnnotationsExtensions 命名空间的那些,其中仅在我尝试提交数据后才包含 Required 和 Email 属性。

下面是我用于 LogOnModel 的代码,它在 MVC 3 下运行良好:

public class LogOnModel 
    { 
        [Required(ErrorMessageResourceType = typeof(YeagerTechResources.Resources), ErrorMessageResourceName = "Required")] 
        [StringLength(50, ErrorMessageResourceType = typeof(YeagerTechResources.Resources), ErrorMessageResourceName = "MinLength6")] 
        [Display(Name = "Username", ResourceType = typeof(YeagerTechResources.Resources))] 
        public string UserName { get; set; } 

        [Required(ErrorMessageResourceType = typeof(YeagerTechResources.Resources), ErrorMessageResourceName = "Required")] 
        [DataType(DataType.EmailAddress)] 
        [Email(ErrorMessageResourceType = typeof(YeagerTechResources.Resources), ErrorMessageResourceName = "Email")] 
        public string Email { get; set; } 

        [Required(ErrorMessageResourceType = typeof(YeagerTechResources.Resources), ErrorMessageResourceName = "Required")] 
        [StringLength(50, ErrorMessageResourceType = typeof(YeagerTechResources.Resources), ErrorMessageResourceName = "MinLength6")] 
        [DataType(DataType.Password)] 
        [Display(Name = "Password4", ResourceType = typeof(YeagerTechResources.Resources))] 
        public string Password { get; set; } 

        [Display(Name = "AcctRemember", ResourceType = typeof(YeagerTechResources.Resources))] 
        public bool RememberMe { get; set; } 
    } 

这是作者的网站:http ://weblogs.asp.net/srkirkland/archive/2011/02/23/introducing-data-annotations-extensions.aspx 以及他对另一个使用它们的人的回复(日期为 2012 年 8 月 10 日) “我已经测试了当前的 NuGet 包可以在 MVC4 中正常工作,同时使用 .NET 4.0 和 4.5。”

下面是我在母版页中对脚本的设置:

<head> 
    <title>@ViewBag.Title</title> 
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> 
    <link href="@Url.Content("~/Content/kendo/2012.2.710/kendo.common.min.css")" rel="stylesheet" type="text/css" /> 
    <link href="@Url.Content("~/Content/kendo/2012.2.710/kendo.dataviz.min.css")" rel="stylesheet" type="text/css" /> 
    <link href="@Url.Content("~/Content/kendo/2012.2.710/kendo.blueopal.min.css")" rel="stylesheet" type="text/css" /> 
    <script src="~/Scripts/jquery-1.7.1.min.js"></script> 
    <script src="~/Scripts/jquery.validate.min.js"></script> 
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> 
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> 
    <script src="~/Scripts/modernizr-2.5.3.js"></script> 
    <script src="~/Scripts/kendo/2012.2.710/kendo.web.min.js"></script> 
    <script src="~/Scripts/kendo/2012.2.710/kendo.aspnetmvc.min.js"></script> 
    <script src="@Url.Content("~/Scripts/kendo/2012.2.710/jquery.min.js")"></script> 
    <script src="@Url.Content("~/Scripts/kendo/2012.2.710/kendo.all.min.js")"></script> 
    <script type="text/javascript"> 
        $(document).ready(function () 
        { 
            $("#editor-field").css("border", "2px solid #f00"); 
            var strErrCode = '@TempData["ErrCode"]'; 
            switch (strErrCode) 
            { 
                case 'CustView': 
                    alert('@(Resources.CustViewErr)'); 
                    break; 
                case 'CustErr': 
                    alert('@(ViewBag.Error)'); 
                    break; 
                default: 
                    { 
                        if (strErrCode != "") 
                            alert(strErrCode); 
                    } 
                    break; 
            } 
        }); 
    </script> 
    <script> 
        function error_handler(e) 
        { 
            if (e.errors) 
            { 
                var message = "Errors:\n"; 
                $.each(e.errors, function (key, value) 
                { 
                    if ('errors' in value) 
                    { 
                        $.each(value.errors, function () 
                        { 
                            message += this + "\n"; 
                        }); 
                    } 
                }); 
                alert(message); 
            } 
        } 
    </script> 
</head> 

任何帮助将不胜感激......我死在水里,没有其他地方可以转身......

4

2 回答 2

1

底线是以下所有内容都适用于 DataAnnotationExtensions & ExtensionsDataAnnotations.ClientValidation dll。

但是,Min & Maxlength Dataannotations 在 MVC4 中不再起作用!它快把我逼疯了!

[Required(ErrorMessageResourceType = typeof(YeagerTechResources.Resources), ErrorMessageResourceName = "Required")]
         [StringLength(50, ErrorMessageResourceType = typeof(YeagerTechResources.Resources), ErrorMessageResourceName = "MinMaxLength", MinimumLength = 6)]
         //[StringLength(10, MinimumLength = 2)]
         //[Range(1, 100)]
         //[Min(3)]
         //[MinLength(2, ErrorMessage = "Name must be more than 5 characters")]
         //[MaxLength(5, ErrorMessage = "Name must be more than 5 characters")]
         [Display(Name = "Username", ResourceType = typeof(YeagerTechResources.Resources))]
于 2012-09-11T19:24:16.850 回答
1

使用正则表达式来验证整数,

[RegularExpression("^[0-9]*$", ErrorMessage = "The number must be an integer")]
public int Number {get; set;} 

我确实认为这是解决问题的最简单方法。

于 2014-04-30T14:37:35.013 回答