ASP.NET MVC 2 RC client side validation not working

后端 未结 5 566
时光说笑
时光说笑 2021-01-19 01:58

I can\'t seem to get any client side validation working on a MVC 2 RC app.

My model has the following:

public class ExampleModel
{
    [Required(Erro         


        
相关标签:
5条回答
  • 2021-01-19 02:34

    Ok I figured this out... and it is 100% my fault. Although, a couple of the posts included some information that I did need also.

    The main problem, which I am suprised no one noticed, was my HTML to include the scripts... look up at my post and see if you can see the problem.

    I was using a <link href=... tag instead of the proper <script src=... tag. Totally my fault as I had quickly cut and pasted the CSS link without thinking and just changed the type and file. Duh!!!

    Anyways the correct links required are:

    <script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script src="/Scripts/MicrosoftMvcJQueryValidation.js" type="text/javascript"></script>
    

    Everything works then. You definately do need to include the 'MicrosoftMvcJQueryValidation.js' file from the futures project so I am upvoted all the posts that mentioned that.

    Out of the box though that file is NOT included. If your not worried about using JQuery then you can just use the following includes to use the Microsoft implementation which will work out of the box with the RC:

    <script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
    <script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>    
    

    I hope I can save at least one person some grief... I still can't believe how I could have screwed up the include and not noticed it for sooooo long.

    Thanks again for all your help.

    0 讨论(0)
  • 2021-01-19 02:40

    You have to include MicrosoftMvcJQueryValidation.js file :

    <link href="MicrosoftMvcJQueryValidation.js" type="text/javascript" />

    Check this: Where is the right version of MicrosoftMvcJQueryValidation.js for MVC 2 beta 2?

    Next just put Html.EnableClientValidation(); somewhere in the View page. It must be before the first form you want to client-side validate. I prefer Site.Master page.

    Working on ASP.NET MVC 2 RC.

    0 讨论(0)
  • 2021-01-19 02:48

    Are you sure you included the correct JS files? Because in Phill Haack's post it has MicrosoftMvcJQueryValidation.js attached instead of MicrosoftMvcValidation.js.

    He also sets the ClientValidationFunction property in the view :

    <% ViewContext.FormContext.ClientValidationFunction 
    = "EnableClientValidation"; %>
    

    Though that was not RC, but Beta.

    0 讨论(0)
  • The default (and only supported by Microsoft) validation system in the ASP.NET MVC 2 Release Candidate does not use jQuery Validate. Instead, it uses a new validation system that exists entirely in MicrosoftMvcValidation.js (though you do also need to include MicrosoftAjax.js).

    If you want to use the jQuery Validate library, it is included as part of the ASP.NET MVC Futures project (available here), which is a separate download, and has its own adapter script file.

    Also, regarding the Html.ValidationSummary() helper, I believe it needs to be included inside the form if you want it to have the new client-side functionality. It will still work if it's outside of the form, but it will only work server-side.

    0 讨论(0)
  • 2021-01-19 02:58

    When you update project from MVC 2 Beta, use: /src/MvcFutures/MicrosoftMvcJQueryValidation.js from MVC 2 RC Source Code Package (link). Older Beta version do not work correctly with jquery.validation in RC. Needed javascript files are:

    <script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript" />
    <script src="/Scripts/jquery.validate.min-vsdoc.js" type="text/javascript" />
    <script src="/Scripts/MicrosoftMvcJQueryValidation.js" type="text/javascript" />
    

    Right version of MicrosoftMvcJQueryValidation.js contains this $(document).ready() function:

    // need to wait for the document to signal that it is ready
    $(document).ready(function() {
        var allFormOptions = window.mvcClientValidationMetadata;
        if (allFormOptions) {
            while (allFormOptions.length > 0) {
                var thisFormOptions = allFormOptions.pop();
                __MVC_EnableClientValidation(thisFormOptions);
            }
        }
    });
    

    at the end of file (in RC release).

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