Muhammad Adeel Zahid
Muhammad Adeel Zahid

Reputation: 17784

Remote Validation in asp.net mvc 3


we are in process of upgrading from mvc2 to mvc3 and are really having trouble with remote validation feature. This is how web.config's appsetting looks like

<appSettings>
    <add key ="ClientValidationEnabled" value ="true"/>
    <add key ="UnobtrusiveJavaScriptEnabled" value ="true"/>
    <add key="CrystalImageCleaner-AutoStart" value="true" />
    <add key="CrystalImageCleaner-Sleep" value="60000" />
    <add key="CrystalImageCleaner-Age" value="120000" />
  </appSettings>

This is how my model and associated metadata looks like

[MetadataType(typeof(setupEmployeeValidator))]
    public partial class setupEmployee { }


    public class setupEmployeeValidator
    {
        [Required(ErrorMessage = "Employee Name is Required")]
        [StringLength(50, ErrorMessage = "Employee Name should be less than {1} characters")]

        public String EmployeeName { get; set; }
        [Required(ErrorMessage = "ID card number is Required")]
        [RegularExpression(@"^\d{5}-\d{7}-\d{1}", ErrorMessage = "Format for CNIC is xxxxx-xxxxxxx-x")]
        [StringLength(16, ErrorMessage = "ID card number should be less than {1} characters")]
        [Remote("CheckDuplicateNIC","hcm","Employee Already Exists")]
        public String CNIC { get; set; }
        [Required(ErrorMessage = "Gender is Required")]
        public String Gender { get; set; }
        [Required(ErrorMessage = "Religion is Required")]
        [StringLength(50, ErrorMessage = "Religion should be less than {1} characters")]
        public String Religion { get; set; }
        public DateTime? DOB { get; set; }
        [Required(ErrorMessage = "Nationality is Required")]
        [StringLength(50, ErrorMessage = "Nationality should be less than {1} characters")]
        public String Nationality { get; set; }
        [Required(ErrorMessage = "Marital Sataur is Required")]
        public String MaritalStatus { get; set; }
        public int ScaleID { get; set; }

    }

i have made sure that required jquery files are loaded on the page in correct order. when i inspect the generated html code i do not find any HTML5 fields generated for remote validation (fields for regex and required validation are present there)

<input type="text" value="" name="CNIC" id="CNIC" data-val-required="ID card number is Required" data-val-regex-pattern="^\d{5}-\d{7}-\d{1}" data-val-regex="Format for CNIC is xxxxx-xxxxxxx-x" data-val-length-max="16" data-val-length="ID card number should be less than 16 characters" data-val="true">

i don't know what could be the problem. i created a sample mvc3 project and in that proejct everything seems to be working fine but when i try to implement it in my application it just does not work. any help is highly appreciated.
thanks

Upvotes: 0

Views: 1307

Answers (1)

Muhammad Adeel Zahid
Muhammad Adeel Zahid

Reputation: 17784

[Remote("CheckDuplicateNIC","hcm","Employee Already Exists")]

above line is the culprit. in MVC2 i created my own validation attribute for remote validation that served as base class for all remote validation attributes and unfortunately with the same name. when i converted to mvc3, the .NET was still passing parameters to old class (my class created for MVC 2). that is why html5 attributes were not being generated for remote validation only.
thanks

Upvotes: 1

Related Questions