Collection of complex child objects in Asp.Net MVC 3 application?

XiaoDarren

New Member
I want to be able to update a model and all its collections of child objects in the same view. I have been referred to these examples: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx and http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/ .For example, I have an object Consultant, that has a collection of "WorkExperiences". All this is in an Entity Framework model. In the view, the simple properties of the Consultant object is no problem, but the collection I cannot get a textbox to show up for. I tried following the examples in the links above, but it doesn't work. The problem is, in those examples the model is just a list (not an object with a child list property). And also, the model again is an EF model. And for some reason that doesn't seem to work as in those examples.Just to make it simple, I tried to do something along the lines of Phil Haacks example, and just get the View to show the textbox:\[code\]@for (int i = 0; i < Model.WorkExperiences.Count; i++){ Html.TextBoxFor(m => m.WorkExperiences.Name);}\[/code\]I tried to create a new WorkExperience object in the controller for the ViewModel:\[code\] public ActionResult Create(int id) { Consultant consultant = _repository.GetConsultant(id); DetailsViewModel vm = new DetailsViewModel(); vm.WorkExperiences = consultant.WorkExperiences.ToList(); vm.WorkExperiences.Add(new WorkExperience()); return View(vm); }\[/code\]But the View doesn't show any empty textbox for the WorkExperience Name property. If on the other hand I create a separate View just for adding a new WorkExperience object, passing a new empty WorkExperience object as the model, this works fine:\[code\]@Html.EditorFor(model => model.Name)\[/code\]That gives me an empty textbox, and I can save the new object. But why can't I do this in the same view as the Consultant object, with collections according to the examples in the links above?BTW, this is sort of a follow-up question to an earlier one, that pointed me to the above links, but I never got to a final solution for it. See that question if more info is needed: Create Views for object properties in model in MVC 3 application?UPDATE:According to answers and comments below, here's an update with the View and an EditorTemplate:The View:\[code\]@model Consultants.ViewModels.DetailsViewModel@{ ViewBag.Title = "Index";}<h2>Index</h2><p> @Html.ActionLink("Add work experience", "CreateWorkExperience", new { id = ViewBag.Consultant.Id })</p><table> <tr> <th></th> <th> Name </th> </tr>@foreach (var item in Model.WorkExperiences) { <tr> <td> @Html.ActionLink("Edit", "Edit", new { id = item.Id }) | @Html.ActionLink("Details", "Details", new { id = item.Id }) | @Html.ActionLink("Delete", "Delete", new { id = item.Id }) </td> <td> @item.Name </td> </tr>}</table>@for (int i = 0; i < Model. WorkExperiences.Count; i++){ Html.EditorFor(m => m. WorkExperiences);}\[/code\](Please note that all this is not really how I'll design it in the end, all I am after right now is to get the WorkExperience object to show up as an empty textbox to fill out, and to be able to add and delete such textboxes as in Phil Haack's and Steven Sanderson's examples.)The EditorTemplate:\[code\]@model [email protected](m => m.Name);\[/code\]This stuff with the EditorTemplate works fine in Phil Haack's sample project, which I downloaded to try, but here, with the EF model or whatever the problem is, I don't get any textbox at all. The table in the view is just there as a test, because in the table I do get the rows for WorkExperiences, whether I add an empty WorkExperience object or fill out its properties doesn't matter, the rows show up for each object. But again, no textbox...
 
Top