ASP.net MVC - Update Model on complex models
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)]
pu开发者_如何学JAVAblic 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/
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();
}
精彩评论