validationattribute检查一个条件中的多个值

本文关键字:条件 检查 validationattribute 一个 | 更新日期: 2023-09-27 17:49:47

我还是ASP新手。. NET MVC和我正在努力与一个验证器,我需要实现。

我的Account模型中有一个UserName属性,需要检查其唯一性。它的声明如下:

[Required(ErrorMessageResourceName = "UserNameRequiredError", ErrorMessageResourceType = typeof(App_LocalResources.Resources))]
[Display(Name = "UserName", ResourceType = typeof(App_LocalResources.Resources))]
[VallidateAccount(Field = AccountChecks.UserNameUnique, ErrorMessageResourceName = "UserNameUniqueError", ErrorMessageResourceType = typeof(App_LocalResources.Resources))]
public string UserName { get; set; }

使用的ValidationAttribute具有以下内容来执行检查

public class VallidateAccountAttribute : ValidationAttribute
{
    public AccountChecks Field { get; set; }
    public int ID { get; set; }
    public override bool IsValid(object value)
    {
        switch (Field){
            case AccountChecks.UserNameUnique:
                return CheckUserName((string)value);
            //Other Fields
            default:
                throw new ParameterException("Invallid Field");
        }
    }
    private bool CheckUserName(string username)
    {
        Account account = Connection.DB.Accounts.FirstOrDefault(x => x.UserName == username);
        return !string.IsNullOrEmpty(username) && (account == null || account.AccountID == this.ID);
    }
    //Other checks
}

现在的问题是:输入的用户名通过IsValid方法的value参数被正确地传递到验证中,但是我找不到一种方法来传递ID。

我试图通过相同的方式传递它,我传递字段参数,但我不能使用this关键字。

[VallidateAccount(Field = AccountChecks.UserNameUnique, ID = this.AccountID, ErrorMessageResourceName = "UserNameUniqueError", ErrorMessageResourceType = typeof(App_LocalResources.Resources))]

谁能帮我通过ID值?

编辑:Rahul RJ提出的实现方案

远程验证使我能够准确地做我想做的事情。

新的UserName属性:

[Required(ErrorMessageResourceName = "UserNameRequiredError", ErrorMessageResourceType = typeof(App_LocalResources.Resources))]
[Display(Name = "UserName", ResourceType = typeof(App_LocalResources.Resources))]
[System.Web.Mvc.Remote("UserNameUnique", "Validation", AdditionalFields = "AccountID", HttpMethod = "POST", ErrorMessageResourceName = "UserNameUniqueError", ErrorMessageResourceType = typeof(App_LocalResources.Resources))]
public string UserName { get; set; }

在这种情况下,ValidationAttribute不再使用,取而代之的是新的ValidationController

using System.Web.Mvc;
using MyProject.Models;
namespace MyProject.Controllers
{
    [OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
    public class ValidationController : Controller
    {
        MyDbContext _repository;
        public ValidationController() : this(Connection.DB) { }
        public ValidationController(MyDbContext repository)
        {
            _repository = repository;
        }
        //NOTE: Make sure the parameters have the same name as your properties!!!
        public JsonResult UserNameUnique(string UserName, int AccountID = 0)
        {
            Account account = Connection.DB.Accounts.FirstOrDefault(x => x.UserName == UserName);
            return Json(!string.IsNullOrEmpty(UserName) && (account == null || account.AccountID == AccountID), JsonRequestBehavior.AllowGet);            
        }
    }
}

validationattribute检查一个条件中的多个值

您可以在此场景中执行远程端验证。你可以很容易地检查现有的用户名。

使用http://msdn.microsoft.com/en-us/library/gg508808(v=vs.98).aspx作为参考

你做得太多了!如果你想验证唯一性,那就这么做。

如果你有。firstordefault,你应该把它改成。any,然后检查是否有任何行与这个用户名。

private bool CheckUserName(string username) {如果(string.IsNullOrEmpty(用户名)返回false;

返回! Connection.DB.Accounts。Any(x => x. username == username);}