asp.net mvc 3中的远程验证

时间:2011-03-14 05:38:03

标签: asp.net-mvc validation asp.net-mvc-3 unobtrusive-validation


我们正在从mvc2升级到mvc3,并且确实遇到了远程验证功能的问题。这就是web.config的appsetting的样子

<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>

这就是我的模型和相关元数据的样子

[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; }

    }

我已确保以正确的顺序在页面上加载了所需的jquery文件。当我检查生成的HTML代码时,我没有找到为远程验证生成的任何HTML5字段(正则表达式和所需验证的字段存在)

<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">

我不知道会出现什么问题。我创建了一个示例mvc3项目,在该项目中,一切似乎工作正常,但当我尝试在我的应用程序中实现它时,它只是无法正常工作。任何帮助都非常感谢。
谢谢

1 个答案:

答案 0 :(得分:1)

  

[远程(“CheckDuplicateNIC”,“hcm”,“员工已存在”)]

以上行是罪魁祸首。在MVC2中,我为远程验证创建了自己的验证属性,该属性用作所有远程验证属性的基类,不幸的是具有相同的名称。当我转换为mvc3时,.NET仍然将参数传递给旧类(我为MVC 2创建的类)。这就是为什么没有为远程验证生成html5属性的原因。
感谢