ASP.NET MVC example of editing multiple child records
Solution 1:
You can try something like this.
Suppose you have this object :
public class Vehicle
{
public int VehicleID { get; set; }
public string LicencePlate { get; set; }
public string Color { get; set; }
}
And this is your controller action that you'll use to edit vehicle details (where you'll post the form) :
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditVehicles(int Owner, Vehicle[] vehicles)
{
//manipulate the data, then return back to the list
return RedirectToAction("YourAction");
}
Then you should set your form this way :
<!--have a form for each person, listing their vehicles-->
<form action="/EditVehicles" method="post">
<input type="hidden" name="Owner" value="25" />
<input type="hidden" name="Vehicles[0].VehicleID" value="10" />
<input type="text" name="Vehicles[0].LicencePlate" value="111-111" />
<input type="text" name="Vehicles[0].Color" value="Red" />
<input type="hidden" name="Vehicles[1].VehicleID" value="20" />
<input type="text" name="Vehicles[1].LicencePlate" value="222-222" />
<input type="text" name="Vehicles[1].Color" value="Blue" />
<input type="submit" value="Edit" />
</form>
This will help the DefaultModelBinder to correctly bind the form data to your model in your controller. Thus Response.Write(vehicles[1].Color);
on your controller, will print "Blue".
This is a very simple example, but I'm sure you get the idea. For more examples about binding forms to arrays, lists, collections, dictionaries, take a look at here.