AnonyMouse
AnonyMouse

Reputation: 18650

MVC why is UpdateModel sometimes used in Edit and sometimes not

I'm new to MVC and I've been looking through a bunch of examples.

For the HttpPost on some edits they call UpdateModel(entity).

In other examples such as:

http://www.asp.net/mvc/tutorials/mvc-music-store-part-5

UpdateModel(entity) isn't called at all.

What's the point of calling this function when it appear unneccessary in MVCMusicStore?

Apparently it " Updates the specified model instance using values from the controller's current value provider."

However I've found from the MVCMusicStore example the updated values are already posted through?

Could someone please explain this to me?

Upvotes: 1

Views: 1189

Answers (3)

Max Toro
Max Toro

Reputation: 28618

Use UpdateModel for PATCH semantics. From RFC5789:

The PATCH method requests that a set of changes described in the request entity be applied to the resource identified by the Request-URI.

This means you are making modifications to an existing resource (e.g. from the database).

Use the object as action method parameter for PUT semantics.

The difference between the PUT and PATCH requests is reflected in the way the server processes the enclosed entity to modify the resource identified by the Request-URI. In a PUT request, the enclosed entity is considered to be a modified version of the resource stored on the origin server, and the client is requesting that the stored version be replaced.

In practice, there's not much difference if the request contains values for all of the resource members. But if the request only contains values for a subset of the resource members, in PATCH the other members are left unmodified, and in PUT are set to their default value (usually NULL).

Upvotes: 1

Muhammad Adeel Zahid
Muhammad Adeel Zahid

Reputation: 17794

i don't think ModelBinding is only introduced in newest version of asp.net mvc (newest version is 3). it was present at least in v-2 so far as i can tell. when you call the updatemodel you call Modelbinding explicitly. when you receive at as action method parameter Modelbinder is called implicitly.
In Edit scenario updateModel is used when we fetch original entity from db and tell controller to update it using UpdateModel like

public ActionResult Edit(int id)
{
   var entity = db.GetEntity(id);
   UpdateModel(entity);
   db.SaveChanges(); 
}

Other scenario is when you are not fetching db entity but ModelBinder gives you an entity created from form fields etc. and you tell you db that object is already there and it has been modified outside the db and you better sync with it like in MusicStore tutorial.

Upvotes: 0

lomaxx
lomaxx

Reputation: 115833

There should be no reason for you to use UpdateModel in the newer version of ASP.NET MVC

Originally it was provided because when you posted your data back to the action on your controller, a FormsCollection would be passed in to to the action and then a call to UpdateModel(entity) would be required.

However in newer versions of ASP.NET MVC the concept of ModelBinding has been introduced which will allow your actions to define a .net object to be passed in to your action methods and the model binder will take care of "binding" the values to the model.

To be completely honest, I'm not sure why they don't just deprecate the UpdateModel() method because AFAIK it's completely redundant.

Upvotes: 1

Related Questions