zoukankan      html  css  js  c++  java
  • MVC4 + EF为Model添加单独的验证属性

    可使用以下方式给Model加上相关的meta验证属性,这样实体的验证属性就不会被例如EF或其他工具自动生成的Model所替换了.

    using System.ComponentModel.DataAnnotations;

    namespace IDO.Entities
    {
     [MetadataType(typeof(CustomerMetadata))]
     partial class Customer
     {
     }

     public class CustomerMetadata
     {
      [Required(ErrorMessage = "{0}不能为空")]
      public string Name { get; set; }

      [Required(ErrorMessage = "{0}不能为空")]
      [StringLength(11, ErrorMessage = "{0}不能超过{1}位")]
      [RegularExpression(@"^1[3458]d{9}$", ErrorMessage = "{0}格式无效")]
      public string Mobile { get; set; }

      [RegularExpression(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$", ErrorMessage = "{0}格式无效")]
      public string Email { set; get; }

      [Required(ErrorMessage = "{0}不能为空")]
      public string Title { get; set; }
     }
    }

  • 相关阅读:
    dede织梦调取一二三级栏目名及栏目下的内容列表的方法
    Maven 加载ojdbc14.jar报错,解决方法
    vue.set动态新增对象属性,触发dom渲染
    object_funs.py
    module+standard library.py
    function.py
    exception.py
    assign.py
    if_test.py
    hello.py
  • 原文地址:https://www.cnblogs.com/itjeff/p/3941527.html
Copyright © 2011-2022 走看看