0

Working in MVC Razor view engine; I've got a model with standard required attributes on properties and a partial view that is rendered through an ajax callback. The main View contains the form, and the partial just contains the display templates etc, in order to trigger the data annotations on the client-side, I had to do the following;

$('#MyButton').click(function () {
        $('#MyContainer').load("MY_ACTION_URL", function () {
            var form = $("#ReserveForm");
            form.removeData('validator');
            form.removeData('unobtrusiveValidation');
            $.validator.unobtrusive.parse(form);                
        });
});

the above works great, it rebinds my client-side validations and they fire perfectly. the problem is that I have some custom Jquery validation which are not firing. For example;

$('#ReserveForm').validate({
        ignoreTitle: true,
        errorLabelContainer: '#errorContainer',
        errorClass: 'ClientSide-Validation-errors'
    });

    $.validator.addMethod('myCustomValidation',
        function (value, element, param) {
            return $(element).val() == '' && param == true;
        }, 'my message');

    $('#MYINPUT').rules("add",
        {
            myCustomValidation: true
        });

The above custom validation is declare on the document ready of the rendered partial but no luck.

PS, if I render the partial as the page loads (in a RenderAction for example) then the custom validations fire fine.

Any help appreciated.

jrummell
  • 42,637
  • 17
  • 112
  • 171
jobe
  • 11
  • 2

1 Answers1

0

I believe this is a duplicate. You do not want to have scripts in your partial view. You should be extracting your scripts and declaring them in the parent view for the partial.

MVC4 partial view javascript bundling Issue

Community
  • 1
  • 1
iAmClownShoe
  • 606
  • 1
  • 4
  • 10