2

I have a view similarly declared on this link.

<div id="personSection">
<!-- We have more than one personspecificmodel -->
 @if (Model.PersonSpecificModels != null)
 {
     for (var i = 0; i < Model.PersonSpecificModels.Count; i++)
     {            
         <div>
             <div class="formColumn2">@Html.EditorFor(x => x.PersonSpecificModels[i].childProperty1)</div>
             <div class="formColumn2">@Html.EditorFor(x => x.PersonSpecificModels[i].childProperty2)</div>
         </div>
     }
 }
 else
 {
     <!-- This is the form for the new user -->
         <div>
             <div class="formColumn2">@Html.EditorFor(x => x.PersonSpecificModels.childProperty1)</div>
             <div class="formColumn2">@Html.EditorFor(x => x.PersonSpecificModels.childProperty2)</div>
         </div>        
 }

The challenge that I have is that I have a model that have the following declaration:

public class PersonModel {
    // Some properties, annotations, etc
    [Required(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "ValidationErrorRequiredField")]
    [StringLength(100, ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "StringLength")]
    public string property1 { get; set; }
    public List<PersonSpecificModel> PersonSpecificModels { get; set; }

    public class PersonSpecificModel {
        // Some declarations further
        public string childProperty1 { get; set; }
        public int childProperty2 { get; set; }
    }
}

Before, there is a 1:1 relationship between PersonModel and PersonSpecificModel. One of the things that we need to do is to allow multiple PersonSpecificModels. Think of having a user having different bank account details.

The code that handles submit of this view is this:

    [HttpPost, ValidateInput(false)]
    public new ActionResult MyAction(PersonModel model)
    {      
        if (ModelState.IsValid) // multiple PersonSpecificModels fail here
        {
        }
    }

There is no issue when I am adding a new person to the model. The challenge that I have right now is that when a user have more than 1 PersonSpecificModels, the validation fails as it can't see properly the collection.

[Question] Would there be any way to properly validate the collection with the existing model definition? The model validation fails on the first line alone.

15
  • Not clear what your question is. Does PersonModel contain a property List<PersonSpecificModel>? How are you generating the view for each PersonSpecificModel? Commented Feb 25, 2015 at 1:00
  • Yes, there is a property that contains List<PersonSpecificModel>. The view is generated similar to the link declared above. Commented Feb 25, 2015 at 1:02
  • So what problem are you having (and you need to include the relevant code in your question, not a link) Commented Feb 25, 2015 at 1:04
  • I have modified the question and made it more specific and clear. The question is how can I possibly validate the multiple PersonSpecificModel using the old 1:1 model? In the previous implementation, 1:1 relationship works well but not 1:many. Commented Feb 25, 2015 at 1:32
  • As an aside, (Model.PersonSpecificModels != null); IEnumerable<T> should be declared as an empty set (rather than null). (See stackoverflow.com/questions/1969993/…) Commented Feb 25, 2015 at 1:37

1 Answer 1

1

You can validate each of them manually and handle as you see fit:

ICollection<ValidationResult> results = new List<ValidationResult>();

bool valid =
    Validator.TryValidateObject(obj, new ValidationContext(obj), results, true);

// results is an out collection parameter containing error messages...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.