MVC4 - binding nested collection

catwoman1459

New Member
I have two classes:\[code\]public class Exercise{ public Guid Id {get;set;} public string Name {get;set;} public List<ExerciseItem> Items {get;set;}}public class ExerciseItem{ public Guid Id {get;set;} public string Name {get;set;} public string Content {get;set;}}\[/code\]I also have my view for creating an Exercise object. There is a button on that view named "Add exercise item" where I dynamically invoke ajax method for returning a partial view for ExerciseItem objects. The view is returned correctly. This view is as follows:\[code\]@model Elang.Models.ExerciseItem<div> <input type="hidden" name="Items.Index" value="http://stackoverflow.com/questions/15589086/@Model.Id" /> <input type="hidden" id="Items@(Model.Id)__Id" name="Items[@Model.Id].Id" value="http://stackoverflow.com/questions/15589086/@Model.Id" /> <input type="text" id="Items@(Model.Id)__Content" name="Items[@Model.Id].Content" class="inputText"/></div>\[/code\]The problem is that when I submit the form and my "Create" method is invoked:\[code\][HttpPost]public ActionResult Create(Exercise exercise){ //add exercise to db //HOWEVER!! //exercise.Items is empty}\[/code\]my Items are null. What am I doing wrong? Can someone give me some advices what should I do to fix the problem?
 
Back
Top