MVC 5客户端非侵入式验证不会停止回发

时间:2019-03-19 16:05:28

标签: c# asp.net-mvc validation

我正在接管一个MVC 5项目,该项目试图使客户端验证正常工作。有人剥离了所有捆绑包,然后手动引用CSS和JS。从那以后,我重新添加了捆绑,并认为我现在已经纠正了所有问题。

但是,一旦我根据发现的文章设置了客户端验证,就无法停止回发。从我发现的有关设置客户端验证的所有内容中,我已经正确设置了它,但是我一定会丢失一些东西。

_Layout.cshtml

<head>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>

<body>
    <div>....main code...</div>

    @Scripts.Render("~/bundles/jquery")        
    @Scripts.Render("~/bundles/bootstrap")
    @Scripts.Render("~/bundles/vendorscripts")
    @RenderSection("scripts", required: false)
</body>

Index.cshtml(页面底部的代码)。这是我正在提交的RequestForm,并试图使其在数据丢失时停止回发。

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


    <script type="text/javascript">

        //turn on datepicker
        $(function () {
            $(".date-picker").datepicker({ format: 'mm/dd/yyyy' });
        });


        $("#RequestForm").submit(function (e) {
            e.preventDefault();

            $("#divLoading").show();

            var url = $("#RequestForm").attr("action");
            var data = $("#RequestForm").serialize();

            $.ajax({
                type: "POST",
                url: url,
                data: data,
                success: function (response) {
                    $("#divLoading").hide();
                    $("#RequestFormPanel").hide();

                    $("#resultsPanel").html(response);

                },
                error: function (xhr) {
                    if (xhr.status == 500) {
                        alert('Error Code: ' + xhr.status + '\nError: Internal Server Error.');
                    }
                    else {
                        alert('Error Code: ' + xhr.status);
                    }
                    $("#divLoading").hide();
                }
            })

        });

    </script>
}

Bundle.config

public class BundleConfig
{
    // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

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

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/respond.js"));

        bundles.Add(new StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.css",
                  "~/Scripts/ui/jquery-ui.css",
                  "~/Content/site.css"));


        bundles.Add(new ScriptBundle("~/bundles/vendorscripts").Include(
                  "~/Scripts/ui/jquery-ui.js",
                  "~/Scripts/fontawesome-all.min.js"));
    }
}

Web.config

<appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

0 个答案:

没有答案