How Does ASP.NET MVC Load jQuery Validation?

时间:2018-03-09 19:11:30

标签: asp.net-mvc jquery-validate unobtrusive-validation

I have a minimal ASP.NET MVC 5 app that was generated via Scaffolding. When I look at the generated _Layout.cshtml file I see a <script> tag toward the bottom of the page that loads jQuery but nowhere in _Layout.cshtm do I see a reference to a jQuery Validation module in a <script> tag.

If I go into Chrome Developer Tools, under the "Sources" tab, I can see that indeed jQuery.validate and jquery.validate.unobtrisuve are loaded! But how can they be loaded if they were not referenced by a <script> tag?

1 个答案:

答案 0 :(得分:1)

如果您查看App_Start BundleConfig.cs,请参阅:

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
            "~/Scripts/jquery.validate*"));

定义了jQuery验证脚本包。某些脚手架页面(例如Login)具有:

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

在视图底部引用。

@RenderSection("scripts", required: false)位于_Layout的底部(如果已定义则会呈现脚本)。{/ p>