I have a partial view which is loaded into JQuery UI Dialog. All the data annotations are firing as expected. But custom validations are not firing.

Model Object:

Public class Model
{
     public IEnumerable<SelectListItem> Primary { get; set; }

            [Required]      

            public string SelectedPrimaryID { get; set; }

            public IEnumerable<SelectListItem> Secondary { get; set; }

            [TwoFieldCompare("SelectedPrimaryID", ErrorMessage = "X and Y cannot be same.")]

            public string SelectedSecondaryID{ get; set; }
    }

Client Validation: Debugger is hitting this method

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)

        {    

            var rule = new ModelClientValidationRule()

            {

                ValidationType = "twofieldcompare",

                ErrorMessage = this.ErrorMessage

            };

            rule.ValidationParameters.Add("dependentproperty", dependentProperty);

            yield return rule;   


        }

In customJquery file, I added below code:

// Data Annotation client side

    $.validator.addMethod(

          'twofieldcompare',

          function (value, element, params) {

              var dependentProperty = params.dependentProperty;

              debugger;

            // Check for the condition and return true or false
          });




    $.validator.unobtrusive.adapters.add(

        'twofieldcompare', ['dependentproperty'],




        function (options) {

            var params = {

                dependentProperty: options.params.dependentproperty
            };

            options.rules['twofieldcompare'] = params;

            options.messages['twofieldcompare'] = options.message;

        });

Question:

debugger is not hitting the client side validation.

All the Jquery libraries are referred as a bundle in Master Layout.

Do I need to add any additional reference to make the client side data annotation work in Jquery UI Dialog?

Thanks in advance for your help.

Related posts

Recent Viewed