package id =“DataAnnotationsExtensions.MVC3”version =“1.1.0.0”targetFramework =“net45”

时间:2012-09-11 01:59:14

标签: asp.net-mvc-4 data-annotations

我在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年10月8日)“我已经测试过当前的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> 

任何帮助都会非常感激......我在这里死了,没有别的地方可以转过来....

2 个答案:

答案 0 :(得分:1)

最重要的是,以下所有内容都与DataAnnotationExtensions&amp; ExtensionsDataAnnotations.ClientValidation dlls。

但是,Min&amp; 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))]

答案 1 :(得分:1)

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

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

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