views:

363

answers:

1

Hi there, I'm struggling myself trying to get the contents of a form which is a complex model and then update the model with that complex model.

My account model has many individuals

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult OpenAnAccount(string area,[Bind(Exclude = "Id")]Account account, [Bind(Prefix="Account.Individuals")] EntitySet<Individual> individuals){

    var db = new DB();
    account.individuals = invdividuals;
    db.Accounts.InsertOnSubmit(account);
    db.SubmitChanges();
}

So it works nicely for adding new Records, but not for update them like:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult OpenAnAccount(string area,[Bind(Exclude = "Id")]Account account, [Bind(Prefix="Account.Individuals")] EntitySet<Individual> individuals){

    var db = new DB();
    var record = db.Accounts.Single(a => a.Reference == area); 
    account.individuals = invdividuals;

    try{
        UpdateModel(record, account); // I can't convert account ToValueProvider()
        db.SubmitChanges();
    }
    catch{
        return ... //Error Message
    }
}

My problem is being how to use UpdateModel with the account model since it's not a FormCollection. How can I convert it? How can I use ToValueProvider with a complex model?

I hope I was clear enough

Thanks a lot :)

UPDATE

That's what I was looking for: http://goneale.com/2009/07/27/updating-multiple-child-objects-and-or-collections-in-asp-net-mvc-views/

A: 

This scenario is not supported unless you have your Account type implement IValueProvider.

That would be some pretty strange MVC though. The model binder should make sense of the HTTP request and translate that to your model not take bind your entities to your other entities.

Upon further inspection I think you're looking for: http://msdn.microsoft.com/en-us/library/dd487246.aspx

Try this:

 try{
    db.Accounts.ApplyCurrentValues(record); 
    db.SubmitChanges();
 }
jfar
It seems weird to me that you can create objects and save the to the database but you're not able to update them just like you've created. Otherwise this would be pretty much limited which do not occur in Rails for example.
ludicco
Your using updatemodel incorrectly. There is nothing weird about this.
jfar