2012-01-26 9 views
0

私はProyectoというモデルをデータベースの最初の方法で作成しました。モデルのアノテーションを追加するには、アノテーションを設定する新しいクラスタイプpartialを作成しましたが、custom oneだけは機能しません。 1 私はMVC3の作業でカスタム検証を行うことができません

[MetadataType(typeof(Proyecto.MetaData))] 
public partial class Proyecto : IValidatableObject 
{ 
    private class MetaData 
    { 
     [Display(Name = "Fecha de Solicitud")] 
     [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] 
     public Nullable<System.DateTime> FechaSolicitud { get; set; } 

     [Required(ErrorMessage="Debe ingresar a un encargado de la información para el proyecto")] 
     [Display(Name="Responsable de la Informacion")] 
     public string ResponsableInformacion { get; set; } 

     [Display(Name="Cliente Nuevo")] 
     public Nullable<bool> ClienteNuevo { get; set; } 

     [Required(ErrorMessage="Debe colocar una explicación que describa la aplicacion a desarrollar")] 
     public string Descripcion { get; set; } 

     [Required(ErrorMessage="Debe Seleccionar un tipo de requerimiento")] 
     public Nullable<byte> IdTipoRequerimiento { get; set; } 

     [Display(Name="Nombre del Cliente")] 
     public string NombreCliente { get; set; } 

    } 

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) 
    { 
     if ((ClienteNuevo == true) && (NombreCliente == String.Empty)) 
     { 
      yield return new ValidationResult("Debe Colocar un nombre al Cliente"); 
     } 
    } 

} 

セカンドクラスにインターフェイスを設定し、この1で1

まず

は、私はインターフェイスを実装してみました:ここに私はこれを達成しようとした方法ですメタデータクラス

[MetadataType(typeof(Proyecto.MetaData))] 
public partial class Proyecto 
{ 
    private class MetaData : IValidatableObject 
    { 
     [Display(Name = "Fecha de Solicitud")] 
     [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] 
     public Nullable<System.DateTime> FechaSolicitud { get; set; } 

     [Required(ErrorMessage="Debe ingresar a un encargado de la información para el proyecto")] 
     [Display(Name="Responsable de la Informacion")] 
     public string ResponsableInformacion { get; set; } 

     [Display(Name="Cliente Nuevo")] 
     public Nullable<bool> ClienteNuevo { get; set; } 

     [Required(ErrorMessage="Debe colocar una explicación que describa la aplicacion a desarrollar")] 
     public string Descripcion { get; set; } 

     [Required(ErrorMessage="Debe Seleccionar un tipo de requerimiento")] 
     public Nullable<byte> IdTipoRequerimiento { get; set; } 

     [Display(Name="Nombre del Cliente")] 
     public string NombreCliente { get; set; } 


     public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) 
     { 
      if ((ClienteNuevo == true) && (NombreCliente == String.Empty)) 
      { 
       yield return new ValidationResult("Debe Colocar un nombre al Cliente"); 
      } 
     } 

    } 
} 

私は何ですかg間違った?

UPDATE

は私が私のMVC3プロジェクトのためにこれを持って国民にのみ、クラスの名前が、まだ動作していないとメタデータクラスのアクセス修飾子...

答えて

0

を変更しようとしましたそれはよく妥当性検証を行います。

[PropertiesMustMatch("NewPassword", "ConfirmPassword", ErrorMessage = "The new password and confirmation password do not match.")] 
    [Authorize(Roles = "Administrators")] 
    public class ChangePasswordModel 
    { 
     [Required] 
     [DataType(DataType.Password)] 
     [DisplayName("Current password")] 
     public string OldPassword { get; set; } 

     [Required] 
     [ValidatePasswordLength] 
     [DataType(DataType.Password)] 
     [DisplayName("New password")] 
     public string NewPassword { get; set; } 

     [Required] 
     [DataType(DataType.Password)] 
     [DisplayName("Confirm new password")] 
     public string ConfirmPassword { get; set; } 
    } 

おそらくあなたは申し訳ありませんが、私はあなたの例のようにそれなしpublic` `への修飾のアクセスを、変更しようとした別の1

[MetadataType(typeof(ProyectoMetaData))] 
public partial class Proyecto 
{ 
    class ProyectoMetaData 
    { 
+0

にこのコード

[MetadataType(typeof(Proyecto.MetaData))] public partial class Proyecto { private class MetaData : IValidatableObject { 

を交換してみてくださいすることができますし、まだ仕事がありません... =( – Jorge

+0

私は2番目のコードブロックを試しましたか? –

+0

はい..私はあなたの例のように試しました... – Jorge

関連する問題