简体   繁体   English

将模型对象的数据注释映射到自定义类对象.net mvc

[英]Mapping Data Annotation of a Model object to Custom Class object .net mvc

i'm wondering how to map A model object Into a more generic class object and saving values of DataAnnotation to specified field. 我想知道如何将模型对象映射到更通用的类对象中,并将DataAnnotation的值保存到指定字段。

My Example Model that for example. 以我的示例模型为例。 I want to map 1 Model object to 1 FieldSetModel object, that contain the list of Model fields with value, and alla metadata provided by DataAnnotation. 我想将1个Model对象映射到1个FieldSetModel对象,该对象包含具有值的Model字段列表以及DataAnnotation提供的alla元数据。

  • Model.Name -> FieldSet.FormModel[1].id 型号名称-> FieldSet.FormModel [1] .id
  • Model.Name ->FieldSet.FormModel[1].name 型号名称-> FieldSet.FormModel [1]。名称
  • Model.(DataAnnotation(MaxLenght) -> FieldSet.FormModel[1].MaxLenght 模型。(DataAnnotation(MaxLenght)-> FieldSet.FormModel [1] .MaxLenght
  • Model.(DataAnnotation(Display) -> FieldSet.FormModel[1].DisplayName 模型。(DataAnnotation(Display)-> FieldSet.FormModel [1] .DisplayName

etc.. 等等..

Model 模型

    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 And i want to map values and DataAnnotationValues to This class related to the View: FieldSetModel我想将值和DataAnnotationValues映射到与View相关的此类:

   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;
    }
}

As you have mentioned you can get attributes with the information from this question: 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();
}

Getting properties and their values is also quite simple using reflection. 使用反射获取属性及其值也非常简单。 You can refer to this question: 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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM