Form validation when a model is a collection of submodel models

I have a view that requires several models to work correctly. So, I created a model, which is a set of several (sub) models. This is a model.

public class PolicyDetail
{
    public Policy Policy { get; set; }
    public IEnumerable<Insured> Insureds { get; set; }
    public IEnumerable<Risk> Risks { get; set; }
    public IEnumerable<Construction> Constructions { get; set; }
}

And here is an example of what one of the submodels looks like, which is the actual entity from the database:

public class Policy
{
    [Key]
    public int PolicyID { get; set; }

    [DisplayName("Policy Number")]
    public Guid PolicyNumber { get; set; }

    [Required(ErrorMessage = "Please enter a valid Effective Date.")]
    [DataType(DataType.DateTime)]
    [DisplayName("Effective Date")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime EffDate { get; set; }

    [Required(ErrorMessage = "Please enter a valid Expiration Date.")]
    [DataType(DataType.DateTime)]
    [DisplayName("Expiration Date")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime ExpDate { get; set; }

    public Boolean IsActive { get; set; }
}

All this worked well, right up until I tried to submit the form with errors to it in order to check the validation. I should have seen this (maybe?), But since the actual model does not have any validation tags, it always passes if (ModelState.IsValid) validation. Is there a way to force or inherit all data annotations from subclasses?

, , ? , / db .

EDIT:

, . , Null Object. :

public class PolicyDetail
{
    [Required, ValidateObject] 
    public Policy Policy { get; set; }
    public IEnumerable<Insured> Insureds { get; set; }
    public IEnumerable<Risk> Risks { get; set; }
    public IEnumerable<Construction> Constructions { get; set; }
}

:

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var results = new List<ValidationResult>();
        var context = new ValidationContext(value, null, null);

        Validator.TryValidateObject(value, context, results, true);

        if (results.Count != 0)
        {
            var compositeResults = new CompositeValidationResult(String.Format("Validation for {0} failed!", validationContext.DisplayName));
            results.ForEach(compositeResults.AddResult);

            return compositeResults;
        }

        return ValidationResult.Success;
    }
}

"" null, :

Validator.TryValidateObject(value, context, results, true);

- ? - ?

+4
1

, : https://msdn.microsoft.com/en-us/library/dd411772.aspx

var context = new ValidationContext(model.Policy, serviceProvider: null, items: null);
var validationResults = new List<ValidationResult>();

bool isValid = Validator.TryValidateObject(model.Policy, context, validationResults, true);

ModelState.AddModelError .

, , , .

+1

All Articles