2

カスタム データ注釈を機能させるのに問題があります。顧客 (CustomerID) の UsergroupName が一意であることを検証する検証属性を追加しようとしています。

[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 をパラメーターとして渡すにはどうすればよいですか?

/M

4

1 に答える 1

2

このアプローチを適用できます
http://byatool.com/mvc/custom-data-annotations-with-mvc-how-to-check-multiple-properties-at-one-time/
検索に ID プロパティを含めるには、同じ UsergroupName を持つすべての「その他の」 UsergroupMetaData をチェックするようにします。

それを確認して、シナリオに適用するのに問題がある場合は、私に連絡してください。

編集:より多くの説明

私の理解では、同じ 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 に答える