MVC 4 client side validation not working

后端 未结 21 1334
情话喂你
情话喂你 2020-12-02 14:00

Can anyone tell me why client side validation is not working in my MVC 4 application.

_layout.schtml

@Scripts.Render("~/bundles/jquery")
@R         


        
21条回答
  •  情歌与酒
    2020-12-02 14:29

    For me this was a lot of searching. Eventually I decided to use NuGet instead of downloading the files myself. I deleted all involved scripts and scriptbundles and got the following packages (latest versions as of now)

    • jQuery (3.1.1)
    • jQuery Validation (1.15.1)
    • Microsoft jQuery Ubobtrusive Ajax (3.2.3)
    • Microsoft jQuery Unobtrusive Validation (3.2.3)

    Then I added these bundles to the BundleConfig file:

    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));
    
    bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate.js",
                    "~/Scripts/jquery.validate.unobtrusive.js",
                    "~/Scripts/jquery.unobtrusive-ajax.js"));
    

    I added this reference to _Layout.cshtml:

    @Scripts.Render("~/bundles/jquery")
    

    And I added this reference to whichever view I needed validation:

    @Scripts.Render("~/bundles/jqueryval")
    

    Now everything worked.

    Don't forget these things (many people forget them):

    • Form tags (@using (Html.BeginForm()))
    • Validation Summary (@Html.ValidationSummary())
    • Validation Message on your input (Example: @Html.ValidationMessageFor(model => model.StartDate))
    • Configuration ()
    • The order of the files added to the bundle (as stated above)

提交回复
热议问题