2

我在让我的自定义数据注释正常工作时遇到问题,我正在尝试添加一个验证属性来验证客户的用户组名称 (CustomerID) 是否唯一。

[MetadataType(typeof(UsergroupMetaData))]
public partial class Usergroup { }

public class UsergroupMetaData
{
    [Required()]
    public object CustomerID { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "UsergroupNameRequired")]
    public object UsergroupName { get; set; }

    [UniqueUsergroupName(????)]
    // what to put here?
}



public class UniqueUsergroupName : ValidationAttribute
{
    UsergroupRepository _rep = new UsergroupRepository();

    public override bool IsValid(object value, int customerID)
    {
        var x = _rep.GetUsergroups().ByUsergroupName(value).ByCustomerID(customerID);

        // what to put here?

        return false;
    }
}

如果“计数 > 0”,IsValid 应该返回 false。

我该如何解决这个问题,使其正常工作。GetUsergroups() 返回 IQueryable。

编辑:

[MetadataType(typeof(UsergroupMetaData))]
public partial class Usergroup { }

public class UsergroupMetaData
{
    public object CustomerID { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "UsergroupNameRequired")]
    [UniqueUsergroupName(ErrorMessageResourceType= typeof(Resources), ErrorMessageResourceName = "UsergroupNameExists")]
    public object UsergroupName { get; set; }


}


public class UniqueUsergroupName : ValidationAttribute
{

    UsergroupRepository _rep = new UsergroupRepository();

    public override bool IsValid(object value, int customerID)
    {

        int usergroups = _rep.GetUsergroups().ByCustomerID(customerID).ByUsergroupName(value.ToString()).Count();

        return usergroups >0;
    }
}

如何将当前的 CustomerID 作为参数传递?

/米

4

1 回答 1

2

您可以应用此方法
http://byatool.com/mvc/custom-data-annotations-with-mvc-how-to-check-multiple-properties-at-one-time/
要在搜索中包含 ID 属性,以便您检查具有相同用户组名称的所有“其他”用户组元数据。

如果您在将其应用于您的场景时遇到问题,请检查并告诉我。

编辑:更多解释

我的理解是你需要检查是否还有其他具有相同UsergroupName的UsergroupMetaData对象。

假设我们将使验证器成为您的整个类而不是属性:

[UniqueUsergroupName]
public class UsergroupMetaData

不需要任何参数。让我们看看 UniqueUsergroupName Validate() 方法的样子:

public override Boolean IsValid(Object value)
{
  var usergroupName = value != null ? value.ToString() : null;
  //We don't validate empty fields, the Required validator does that
  if(string.IsNullOrEmpty(usergroupName)) return true;

  Type objectType = value.GetType();
  //Get the property info for the object passed in.  This is the class the attribute is
  //  attached to
  //I would suggest caching this part... at least the PropertyInfo[]
  PropertyInfo[] neededProperties =
    objectType.GetProperties();

  var customerIdProperty = neededProperties
    .Where(propertyInfo => propertyInfo.Name == "CustomerID")
    .First();
  var customerId = (int?) customerIdProperty.GetValue(value, null);

  var usergroupNameProperty = neededProperties
    .Where(propertyInfo => propertyInfo.Name == "UsergroupName")
    .First();
  var usergroupName = (string) customerIdProperty.GetValue(value, null);

  // Now I don't userstand why the blog post author did all this reflection stuff to 
  //   get the values of the properties. I don't know why he just didn't d something like:
  // var usergroup = (Usergroup) value;
  // var customerId = usergroup.CustomerId;
  // var usergroupName = usergroup.UsergroupName;
  //
  //I think reflection was not needed here. Try both ways anyway.
  // The next lines should not be different regardless of whether you used reflection.
  // 

  //We don't validate empty fields, the Required validator does that
  if(string.IsNullOrEmpty(usergroupName)) return true;

  //Now you have the customerId and usergroupName. Use them to validate.
  //If I'm using LINQ (for explanation only) it'd be something like:
  // Assuming _rep.GetUsergroups() returns IQueryable (only for explanation):
  int numberOfOtherUsergroupsWithSameName = 
        _rep.GetUsergroups()
              .Where(g => g.UsergroupName == usergroupName && g.CustomerId != customerId)
              .Count();
  return numberOfOtherUsergroupsWithSameName == 0;
}
于 2010-01-11T11:24:42.987 回答