MVC 4 client side validation not working

后端 未结 21 1268
情话喂你
情话喂你 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)
    0 讨论(0)
  • 2020-12-02 14:30
    <script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
    <script src="/Scripts/jquery.validate.js"></script>
    <script src="/Scripts/jquery.validate.unobtrusive.js"></script>
    

    This code worked for me.

    0 讨论(0)
  • 2020-12-02 14:32

    I created this html <form action="/StudentInfo/Edit/2" method="post" novalidate="novalidate"> where the novalidate="novalidate" was preventing the client-side jQuery from executing, when I removed novalidate="novalidate", client-side jQuery was enabled and stared working.

    0 讨论(0)
  • 2020-12-02 14:33

    I had an issue with validation, the form posts then it validates,

    This Doesn't work with jquery cdn

        <script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.js"></script>
        <script src="~/Scripts/jquery.validate.min.js"></script>
        <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
    

    This Works without jquery cdn

    <script src="~/Scripts/jquery-1.7.1.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
    

    Hope helps someone.

    0 讨论(0)
  • 2020-12-02 14:34

    the line below shows you haven't set an DataAttribute like required on AgreementNumber

    <input id="AgreementNumber" name="AgreementNumber" size="30" type="text" value="387893" />
    

    you need

    [Required]
    public String AgreementNumber{get;set;}
    
    0 讨论(0)
  • 2020-12-02 14:34

    Use:

    @Html.EditorFor

    Instead of:

    @Html.TextBoxFor

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