asp.net mvc custom modelbinder - how to perfom updates with assosciated entities

310 Views Asked by At

I am trying to understand how associated entities are updated when using custom modelbinders.

If you have a Product entity with a relationship to a Category entity and you are displaying a list of category choice for a product in a dropdown on a form.

The the user assigns a new category and that change needs to be persisted with the Product. How is the binding implemented to assign the updated category? The properties f the Product are easy enough, but how do you set the Product.Category = category?

Hope that is clear :-)

1

There are 1 best solutions below

1
RailRhoad On

Sounds like your custom model binding is there, you're just trying to setup up your relationship between your Product and Category.

To do this you would do something like this:

product.CategoryReference.EntityKey = new EntityKey("Context.Category", "ID", categoryID);

That will simply update the foreign key relationship in your entity.