MVC自定义dataannotation [英] MVC Custom dataannotation

查看:114
本文介绍了MVC自定义dataannotation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这个类如下创建两个自定义dataannotation

i create two custom dataannotation for this class as follow

public class users
{
    public int Id { set; get; }
    [Required(ErrorMessage = "username is Required")]
    [usernameValidation(ErrorMessage= "Sorry this name is already exist")]
     // [MaxLength(ma)]
    public string username { set; get; }

    [Required(ErrorMessage = "Password is required")]
    [DataType(DataType.Password)]
    public string password { set; get; }

    [Required(ErrorMessage = "Confirm Password is required")]
    [StringLength(255, ErrorMessage = "Must be between 5 and 255 characters", MinimumLength = 5)]
    [DataType(DataType.Password)]
    [Compare("password")]
    public string confirmPassword { set; get; }

    [Required(ErrorMessage = "email is required")]
    [DataType(DataType.EmailAddress)]
    [emailValidation(ErrorMessage = "Sorry this e-mail is already exist")]
    public string email { set; get; }

    [Required]
    public int type { set; get; }

    public string photopath { set; get; }

    [DataType(DataType.MultilineText)]
    public string address { set; get; }

    [DataType(DataType.MultilineText)]
    public string note { set; get; }
}

和自定​​义类是

public class emailValidation : ValidationAttribute
{
    db_context db = new db_context();
    public override bool IsValid(object value)
    {
        int query = (from res in db.users
                     where res.email == value.ToString() 
                     select res).Count();
        if (query == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

和其他为

public class usernameValidation : ValidationAttribute
{
    db_context db = new db_context();
    public override bool IsValid(object value)
    {
        int query = (from res in db.users
                     where res.username == value.ToString() 
                     select res).Count();
        if (query == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

这个注释效果很好,当我创建新的用户,

this annotation worked well when i create new user,

但问题是,当我更新的用户,并没有更新的用户名或电子邮件我自定义的注释执行,那么我给我的错误,因为用户名和电子邮件数据库中已存在。

推荐答案

您自定义的 ValidationAttribute 类简单的检查,如果任何用户具有相同的用户名电子邮件,其中包括电流用户,这将导致验证在编辑方案失败。既然你正试图prevent复制用户名电子邮件的实施需要知道的<$的C $ C>编号用户> 正在保存。

Your custom ValidationAttribute classes are simply checking if any users have the same username or email, which includes the current user, which causes validation to fail in the edit scenario. Since you are attempting to prevent duplicate username and email, the implementation needs to be aware of the Id of the user it is saving.

要做到这一点,最简单的方法是通过 的DbContext。执行validateEntity ,因为它看起来像您正在使用实体框架。如果你不使用实体框架,你可以在MVC中使用 ValidationContext ValidationAttribute.IsValid 。见<一href=\"http://odeto$c$c.com/blogs/scott/archive/2011/02/21/custom-data-annotation-validator-part-i-server-$c$c.aspx\" rel=\"nofollow\">http://odeto$c$c.com/blogs/scott/archive/2011/02/21/custom-data-annotation-validator-part-i-server-$c$c.aspx

The simplest way to do this is via DbContext.ValidateEntity, since it looks like you are using Entity Framework. If you are not using Entity Framework, you can use ValidationContext in ValidationAttribute.IsValid in MVC. See http://odetocode.com/blogs/scott/archive/2011/02/21/custom-data-annotation-validator-part-i-server-code.aspx

这篇关于MVC自定义dataannotation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆