asp.net mvc client side validation not working?

前端 未结 6 1710
挽巷
挽巷 2021-01-04 03:59

For some reason my client side validation does not seem to be working:

Here is my html:

@using (Html.BeginForm(\"Create\", \"Home\", FormMethod.Post)         


        
6条回答
  •  春和景丽
    2021-01-04 04:47

    Not sure if this is being loaded dynamically (e.g loading a partial view with Ajax). If so then you need to parse the html with the validator on the success e.g. Use something like...

    $.validator.unobtrusive.parse(".target");
    

    E.g.

     function loadAPartialView(endPoint) {                    
                    $.ajax({
                        url: endPoint,
                        type: 'GET',
                        cache: false,
                        success: function (result) {
    
                            $('.target').html(result);
                            $('.target').show();
    
                            // IMPORTANT. Next line is required to get the client side validation to run.
                            $.validator.unobtrusive.parse(".target");
                            $(".loadingMessage").hide(); 
                        },
                        error: function (result) {
                               // Error message... 
                               $(".loadingMessage").hide();
    
                        }
                    });
                };
    

提交回复
热议问题