1

I have a view with two models: generic account e profile user.

public class AccountModel
{
    [Display(Name = "UserId", Prompt = "UserId", ResourceType = typeof(Strings))]
    public string UserId { get; set; }

    [Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(Validation))]
    [EmailAddress(ErrorMessage = null, ErrorMessageResourceName = "InvalidEmail", ErrorMessageResourceType = typeof(Validation))]
    [Display(Name = "Email", Prompt = "Email", ResourceType = typeof(Strings))]
    public string Email { get; set; }

    [Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(Validation))]
    [StringLength(100, ErrorMessageResourceName = "FieldMaximumLength", ErrorMessageResourceType = typeof(Validation))]
    [Display(Name = "DisplayName", Prompt = "DisplayName", ResourceType = typeof(Strings))]
    public string DisplayName { get; set; }

    [Display(Name = "PhoneNumber", Prompt = "PhoneNumber", ResourceType = typeof(Strings))]
    public string PhoneNumber { get; set; }

    [Display(Name = "Country", Prompt = "Country", ResourceType = typeof(Strings))]
    public int CountryId { get; set; }

    public SelectList CountryList { get; set; }

}

public class UserProfileModel
{
    [Display(Name = "ID User", Prompt = "ID User")]
    public string IDUser { get; set; }

    [Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(Validation))]
    [StringLength(100, ErrorMessageResourceName = "FieldMaximumLength", ErrorMessageResourceType = typeof(Validation))]
    [Display(Name = "UserFullName", Prompt = "UserFullName")]
    public string UserFullName { get; set; }

    [Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(Validation))]
    [StringLength(16, ErrorMessageResourceName = "FieldMaximumLength", ErrorMessageResourceType = typeof(Validation))]
    [Display(Name = "Code", Prompt = "Code")]
    public string Code { get; set; }

    [Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(Validation))]
    [StringLength(250, ErrorMessageResourceName = "FieldMaximumLength", ErrorMessageResourceType = typeof(Validation))]
    [Display(Name = "Birth Place", Prompt = "BirthPlace")]
    public string BirthPlace { get; set; }

    [Display(Name = "Newsletter")]
    public bool? Newsletter { get; set; }

    [Display(Name = "Avatar", Prompt = "Avatar")]
    public string Avatar { get; set; }

}

I should validate the second model only if at least one field connected to him has been filled.
If for example BirthPlace is entered then also validate the second model, otherwise validate only the first model.
I hope my request is clear

Massi
  • 57
  • 7

1 Answers1

0

If I understand your question, probably you want to know how to manually validate the model.

You can use TryValidateModel(modelInstance); call to manually validate the required model.

For other conditions you have mentioned in the question, you can add if statements.

e.g. if all string properties of userProfileModelObject are NULL then validate accountModelInstance

bool isSecondObjectValid = userProfileModelObject.GetType().GetProperties()
    .Where(pi => pi.PropertyType == typeof(string))
    .Select(pi => (string)pi.GetValue(myObject))
    .Any(value => string.IsNullOrEmpty(value));


bool validationResult = false;
validationResult = !isSecondObjectValid ? 
                          TryValidateModel(accountModelInstance) :
                          TryValidateModel(userProfileModelObject);

I hope this helps.

References:

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
  • Thanks for the help, but how do I pass 2 models to the controller? – Massi Mar 19 '19 at 22:25
  • 1
    You will have to encapsulate both of them in single model and then pass it in the Post Request as JSON. Please note that post request does not take two [FromBody] parameters. – Manoj Choudhari Mar 20 '19 at 07:38
  • Why Post Request as JSON? – Massi Mar 21 '19 at 11:48
  • 1
    You have only two ways - either send info in query string or post it. Better to post it as the details would then be in HTTP body and not in URL. JSON because your APIs generally use JSON format for interacting with client. Hope this helps. – Manoj Choudhari Mar 21 '19 at 13:11