Unknown
Unknown

Reputation: 1397

MVC nested model collection is empty (not null)

I have a model that has a property which is a collection. I can successfully bind from the edit actions, for example:

    [HttpGet]
    public ActionResult Edit(string id)
    {
        // code here
        return this.View(complexModel);            
    }

    [HttpPost]
    public ActionResult Edit(ComplexModel complexModel)
    {
        // code here
        return RedirectToAction("AnotherAction")            
    }

In the post method I can successfully receive all the object properties, including the collection one. However I have another view that can invoke the Edit action. When this happens I can see that the html rendered is the same (i.e. the nested property info is there). When I save the changes in the post Edit I receive all the correct properties with one exception - the collection propery has zero items.

Where do I have to search for the problem?

Update: I am properly iterating through the collection and displaying all items with EditorFor; however when coming from a differnt view (the different view is in another controller and the Edit link is placed in a Display Template - if that makes any difference) with the same exact model I can see that the html is the same since all the properties of the collection are there.

Upvotes: 0

Views: 395

Answers (2)

Unknown
Unknown

Reputation: 1397

Ok, false alarm. I figured it out - I am invoking the action method using @Html.ActionLink; I was passing the whole model instead of just the id. I do not know for what silly reason things got messed up, but they are ok now.

Upvotes: 1

Mike Richter
Mike Richter

Reputation: 329

What does your Edit view look like? You need to iterate over all the items in the collection as form elements.

Upvotes: 0

Related Questions