2014-11-19 6 views
0

모델 개체를보다 일반적인 클래스 개체에 매핑하고 DataAnnotation의 값을 지정된 필드에 저장하는 방법을 궁금합니다.모델 개체의 데이터를 사용자 지정 클래스 개체에 매핑 .net mvc

예 예를 들면. 하나의 Model 객체를 1 FieldSetModel 객체에 매핑하고, 값이있는 Model 필드 목록과 DataAnnotation에서 제공하는 alla 메타 데이터를 포함하고자합니다.

  • Model.Name -> FieldSet.FormModel [1] .ID
  • Model.Name -> FieldSet.FormModel [1] .NAME
  • 모델 (DataAnnotation (MaxLenght.) -> FieldSet.FormModel [1]
  • 모델 .MaxLenght (DataAnnotation (디스플레이) -.> FieldSet.FormModel [1] .DisplayName

모델 012,351

6,
public virtual Int32 Id { get; protected set; } 
    #region Login 
    [Required,MaxLength(30)] 
    [Display(Name = "Nome Utente")] 
    public virtual string UserName { get; set; } 

    [Required, MaxLength(200),DataType(DataType.EmailAddress)] 
    [Display(Name = "Email")] 
    public virtual string Email { get; set; } 

    [Required] 
    [Display(Name = "Password"),DataType(DataType.Password),MinLength(6)] 
    [StringLength(100, ErrorMessage="La lunghezza di {0} deve essere di almeno {2} caratteri.", MinimumLength=6)] 
    public virtual string Password { get; set; } 
    #endregion 

FieldSetModel 그리고 내가보기에 관련이 클래스에 값과 DataAnnotationValues을 매핑 할 :

public class FieldSetModel 
{ 
    public string Title; 
    public List<Field> FormModel = new List<Field>(); 

} 

public class Field{ 
    public string Id   { get; private set; } 
    public string Name   { get; private set; } 
    public string DisplayName; 
    public string DataType = "text"; 
    public int MaxLenght = 100; 
    public int MinLenght = 0; 
    public string FormatErrorMessage = "Formato non valido"; 
    public string RangeErrorMessage = "Range non valido"; 
    public string RequiredErrorMessage = "Valore Non Corretto"; 
    public string Pattern; 
    public string DisplayFormat; 

    public string Value; 
    public bool Error; 

    public Field(string name) 
    { 
     this.Name = name; 
     this.Id = name; 
    } 
} 
+0

당신은 아마 그것을 위해 내가이 게시물을 발견했습니다, 당신은 –

+0

이 확인 모델에서 데이터 주석 값을 얻기 위해 반사를해야합니다 : http://stackoverflow.com/questions/ 7027613/how-to-retrieve-data-annotations- 코드에서 - 프로그래밍 방식으로,하지만 지금은 어떻게 모든 모델의 우수성을 엿볼 수 있습니까? – andrea

+0

다음 질문을 참조하십시오. http://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class –

답변

0

당신은 당신이이 질문의 정보와 속성을 얻을 수 있습니다 언급 한 바와 같이 : How to retrieve Data Annotations from code? (programmatically)

public static T GetAttributeFrom<T>(this object instance, string propertyName) where T : Attribute 
{ 
    var attrType = typeof(T); 
    var property = instance.GetType().GetProperty(propertyName); 
    return (T)property .GetCustomAttributes(attrType, false).First(); 
} 

속성 및 값을 가져 오는 것은 리플렉션을 사용하여 매우 간단합니다. 당신은이 질문을 참조 할 수 있습니다 : How to get the list of properties of a class?

class Foo { 
    public int A {get;set;} 
    public string B {get;set;} 
} 
... 
Foo foo = new Foo {A = 1, B = "abc"}; 
foreach(var prop in foo.GetType().GetProperties(BindingFlags.Public)) { 
    Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(foo, null)); 
}