ASP.NET MVC implement custom validator use IClientValidatable

前端 未结 1 1579
夕颜
夕颜 2020-11-27 21:19

I ask similar question here but in this question I use another implementation, exactly this way the following codes show my implementations:

Model:<

相关标签:
1条回答
  • 2020-11-27 21:49

    You have messed up your script inclusions. In your _Layout you have included the following scripts in that order:

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

    Now obviously jquery.validate.min.js and jquery.validate.js represents the same script, the first being the minified version. But since you haven't included the jquery.validate.unobtrusive.js script (this is done much later in your view), your custom jQuery.IsDateAfter.js script will contain errors since it will not know about the $.validator.unobtrusive.adapters object that you are using. So here's how the scripts in your layout should look:

    <script src="@Url.Content("~/Scripts/jquery-1.5.1.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>
    

    You could also add your custom jQuery.IsDateAfter.js script to the layout at the end if you wish in case it is used in many views and if not you could add it to the view:

    <script src="@Url.Content("~/Scripts/jQuery.IsDateAfter.js")" type="text/javascript"></script>
    

    That's the only script you should have in the view. You should remove any other jquery.* script inclusions from your Edit And Create View pages.

    Remark: you will also notice that I have removed all Microsoft*.js scripts from your Layout. They are obsolete and should no longer be used in ASP.NET MVC 3.

    0 讨论(0)
提交回复
热议问题