The TryUpdateModel
method can be used to assign values to an object, using a mapping between it's property-names and the values specified in the web-request. That being said, you can achieve the same behavior by doing something like this:
[HttpPost]
public ActionResult Edit(int id, FormCollection form)
{
Car entity = new Car { Id = id };
// This will attach the car entity to the content in the unchanged state.
this.EntityFrameworkObjectContext.Cars.Attach(car);
TryUpdateModel(entity, form.ValueProvider.ToXYZ()); // Can't remember the exact method signature right now
this.EntityFrameworkObjectContext.SaveChanges();
...
}
When attaching an entity to the context, you're basically just notifying the context that you have an entity, which he should take care of. Once the entity is attached, the context will keep track of the changes made to it.
But this method is only suitable for scalar properties, since navigation properties are not updated using this method. If you have foreign-key properties enabled (like a Product assigned to a Category also has a CategoryId property, which links the two), you should still be able to use this method (since the navigation property is mapped using a scalar property).
Edit: Another way is to accept a Car instance as parameter:
[HttpPost]
public ActionResult Edit(int id, Car car)
{
Car entity = new Car { Id = id };
// This will attach the car entity to the content in the unchanged state.
this.EntityFrameworkObjectContext.Cars.Attach(entity);
// It is important that the Car instance provided in the car parameter has the
// the correct ID set. Since the ApplyCurrentValues will look for any Car in the
// context that has the specified primary key of the entity supplied to the ApplyCurrentValues
// method and apply it's values to those entity instance with the same ID (this
// includes the previously attached entity).
this.EntityFrameworkObjectContext.Cars.ApplyCurrentValues(car);
this.EntityFrameworkObjectContext.SaveChanges();
...
}
You could also roll your own ModelBinder that actually has a reference to your Entity Framework Context and looks whether an Car.Id was specified in the form/query. If an ID is present you can grab the entity to update directly from the context. This method requires some effort, since you must make sure that you search for ID first and then apply all specified property-values. If you are interested I can give you some examle for this.