asp.net mvc 3 中的远程验证

发布于 2024-10-21 22:43:26 字数 2471 浏览 4 评论 0原文


我们正在从 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 项目,在该项目中,一切似乎都工作正常,但当我尝试在我的应用程序中实现它时,它就不起作用。非常感谢任何帮助。
谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪推晚风 2024-10-28 22:43:26

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

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

谢谢

[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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文