How to ignore missing fields from the MetadataType attribute?

I have DTOs that are mapped to ViewModels. To avoid the need to use validation attributes (and other attributes), I wanted to write validation attributes for all properties in the same class and reuse them in my ViewModels. However, when I try to use metadata in a ViewModel that does not have all the DTO properties (all of them really ...), it gives me an exception System.InvalidOperationException.

An exception:

Le type de métadonnées associé pour le type 'MyProject.EntityViewModel' contient les propriétés ou champs inconnus suivants : AnotherProperty. Vérifiez que les noms de ces membres correspondent aux noms des propriétés du type principal.

Google:

The type associated metadata for type 'MyProject.EntityViewModel' contains the following unknown properties or fields: AnotherProperty. Verify that the names of these members match the names of the properties of the main type.

A simplified example:

public class Entity {
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
}

public class EntityDTO {
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
}

//This class is used to add validation attributes for input-related view models
public class EntityInputValidation {
    [Required]
    public string A { get; set; }

    [Required]
    public string B { get; set; }

    //Notice that we dont have a validation for C
}

//This class is a ViewModel used to create a new Entity
[MetadataType(typeof(EntityInputValidation))]
public class EntityCreateViewModel {
    //Required because we use the InputValidation metadata
    public string A { get; set; }

    //Notice that we do not have the B property here, even if we are using the Input Validation which has a required attribute for this property. This is where the exception comes from.

    //C is only required in this View/ViewModel
    [Required]
    public string C { get; set; }
}

Since EntityViewModel does not have AnotherProperty, it throws an exception. Is there any way to prevent this?

+1
source share
1 answer

, , . , , , . , , .

, , , , .

20 , ... 2 -, 3, , DAL -. , .

, , , , . , , , , .

, . , , , , .

:

public class LoginValidation
{
    [Required]
    public string Username { get; set; }
    [Required]
    public string Password { get; set; }
}

:

public class ViewModelA : LoginValidation
{
    public string SomeOtherProperty { get; set; }
}

. , ? , . :

:

  • DataAnnotations.Validator
  • ASP.Net MVC

    : , , . ?

(: http://social.msdn.microsoft.com/Forums/en-US/1748587a-f13c-4dd7-9fec-c8d57014632c/code-first-dataannotations-in-interfaces?forum=adonetefx)

, LoginValidation ? , , :

public class LoginAndDateValidation : LoginValidation
{
    [Required]
    public DateTime StartDate { get; set; }
    [Required]
    public DateTime EndDate { get; set; }
}

public class ViewModelA : LoginAndDateValidation
{
    public string SomeOtherProperty { get; set; }
}

, ? . , , , , , , .

, Mystere Man, , : fooobar.com/questions/345177/...

+2

Source: https://habr.com/ru/post/1538720/


All Articles