The TryUpdateModel method can be used to assign values to an object using the mapping between its property names and the values specified in the web request. With this, 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 };
When you attach an object to a context, you basically just notify the context in which you have an entity that it should take care of. Once the object is attached, the context will track 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 (for example, a Product assigned to a category also has a CategoryId property that binds them), you can still use this method (since the navigation property is displayed using the scalar property).
Edit: Another way is to accept the Car instance as a parameter:
[HttpPost] public ActionResult Edit(int id, Car car) { Car entity = new Car { Id = id };
You can also collapse your own ModelBinder, which actually refers to your Entity Framework context and checks if Car.Id has been specified in the form / request. If an identifier is present, you can capture the object to update directly from the context. This method requires some effort, since you must first make sure that you are looking for an identifier, and then apply all the specified property values. If you are interested, I can give you some examples for this.
source share