ASP.MVC ModelBinding Behaviour

Posted by OldBoy on Stack Overflow See other posts from Stack Overflow or by OldBoy
Published on 2010-02-03T11:30:22Z Indexed on 2010/06/13 23:32 UTC
Read the original article Hit count: 144

Filed under:
|

This one has me stumped, despite the numerous posts on here.

The scenario is a basic MVC(2) web application with simple CRUD operations. Whenever the edit form is submitted and the UpdateModel() called, an exception is thrown:

System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException was unhandled by user code

This occurs against a DropDownList value which is a foreign key on the entity table. However, there is another DropDownList list on the form, representing another foreign key, which does not throw the error (unsurprisingly). Changing the property values manually inside the Edit Action:

Recipe recipe = repository.GetRecipe(int.Parse(formValues["recipeid"]));
recipe.CategoryId = Convert.ToInt32(formValues["CategoryId"].ToString());
recipe.Page = int.Parse(formValues["Page"].ToString());
recipe.PublicationId=Convert.ToInt32(formValues["PublicationId"].ToString());

Allows the CategoryId and Page properties to be updated, and then the error is thrown on the PublicationId. All of the referential integrity is checked an the same in the db and the dbml.

Any light shed on this would be most welcome.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about linq-to-sql