Paweł Staniec
Paweł Staniec

Reputation: 3181

MVC3 Edit object containing collection

I'm trying to make Edit form and controller action for the following Model work :

public class Form {
    public int ID {get;set;}
    public List<FormElement> formElements {get;set;}
}
public class FormElement {
    public int ID {get;set;}
    public int FormID {get;set;}
    public string Question {get;set;}
    public string Answer {get;set;}
}

I've created Edit view for the Form model, used EditorTemplate for FormElement. It looks ok, form elements get rendered properly but when I try to submit the form I get :

The model of type 'testApp.Models.Form' could not be updated.
Line 35:         {
Line 36:             var form = db.Forms.Single(f => f.ID== id);
Line 37:             UpdateModel(form, collection); // <- highlighted

The Create action worked like a charm done pretty much the same way - I'm able to create new objects with collection of other objects as it's property. So I'm not sure why Edit doesn't work the same way ... any ideas ?

UPDATE

After several attempts to achieve the goal - having my IEnumerable of FormElement updated I've found this article http://www.codetuning.net/blog/post/Binding-Model-Graphs-with-ASPNETMVC.aspx that is describing what is happening and how to solve it.

Upvotes: 1

Views: 2126

Answers (1)

Bala R
Bala R

Reputation: 108957

Try

TryUpdateModel(form, collection);

EDIT:

Also see this post.

Upvotes: 1

Related Questions