简体   繁体   English

自定义客户端验证(属性)不会在MVC 4中触发

[英]The custom client-side validation(attribute) doesn't trigger in MVC 4

I write a code for validate the incoming data. 我编写了用于验证传入数据的代码。

All of the built-in validation (RequiredAttribute etc) are works ... 所有的内置验证 (RequiredAttribute等)都可以使用 ...

But my own writed DateRangeAttribute doesnt triggers errors on post , and the post is comes to my controller :(. 但是我自己写的DateRangeAttribute 不会在post上触发错误 ,并且post到达我的控制器:(。

The overrided IsValid method sends back the good(false) value, and on the server side the controller see correctly the Invalid ModelState, but the client side not. 重写的IsValid方法发送回good(false)值,并且在服务器端,控制器可以正确看到Invalid ModelState,而在客户端则看不到。

The data validation attributes are rendered to HTML on the browser: 数据验证属性在浏览器上呈现为HTML:

I would like the avoid the post until all of the data are realy valid. 我想避免发布,直到所有数据都真正有效为止。

There is the Code: 有代码:

DateRangeAttribute DateRangeAttribute

public class DateRangeAttribute : RangeAttribute
    {
        public DateRangeAttribute (string minimumYear) 
            :base(typeof(DateTime), minimumYear, DateTime.Now.Year.ToString())
        {
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var inputYear = value as int?;
            var result = (inputYear != null && inputYear >= 1886 && inputYear <= DateTime.Now.Year);

            return result ? ValidationResult.Success : new ValidationResult(GetErrorMessage());
        }

        private string GetErrorMessage()
        {
            return "The year must be greater than 1886 and lower than the actual year.";
        }

        public override string FormatErrorMessage(string name)
        {
            return string.Format(GetErrorMessage(), name);
        }
    }

Create.cshtml Create.cshtml

<script src="@Url.Content("~/Scripts/jquery-1.8.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<!-- ... -->

@using (Html.BeginForm("Create", "Car", FormMethod.Post, new { id = "CreateForm" }))
{ 
    @Html.ValidationSummary(true)
    <!-- ... -->
        @Html.LabelFor(model => model.Year)
        @Html.EditorFor(model => model.Year)
        @Html.ValidationMessageFor(model => model.Year)
    <!-- ... -->
}

Web.Config file:
<configuration>
    <appSettings>
        <!-- ... -->
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>
    <!-- ... -->
</configuration>

You also have to provide a custom client side validation implementation thru IClientValidatable . 您还必须通过IClientValidatable提供自定义客户端验证实现。 And because of that, I suggest that you inherit from ValidationAttribute directly since you will have to change almost everything anyway 因此,我建议您直接从ValidationAttribute继承,因为无论如何您都必须更改几乎所有内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM