I'm trying to figure out how to iterate through a JSON object in an ASP.NET MVC controller. Every example I've come accross looks like the following where the posted JSON is assumed to contain a single object. However, let's say in my example below I want my controller to handle a collection of people rather than a single person. My inputModel therefore would contain a bunch of Names and Ages. For example, {"Name": "Bob", "Age": "30"},{"Name": "Paul", "Age": "19"}. How would you write your controller code to iterate through the people in the inputModel saving each one to that database?
Controller Code
[HttpPost]
public ActionResult Save(PersonInputModel inputModel) {
try{
Person person = new Person();
person.Name = inputModel.Name;
person.Age = inputModel.Age;
Add(person);
Save(person);
}
catch {
//handle the error
}
}