tags:

views:

58

answers:

2

I'm trying to google for info on a situation, but I dont know what it is called, so its hard to find results :)

I have a model with say 10 fields. But only some of those are shown on a particular view, lets say 3 of them: id, name, date. What do you call this kind of view that does not display the whole model? A partial view?

The problem is that because 7 fields are not sent to the view, when the Update action is called on the controller, those fields are null, and the DB gets updated with those 7 fields set to null.

A: 

This is called a ViewModel, which is obtained from the Model and is more adapted to the View.

Darin Dimitrov
Thanks that was the terminology I was looking for. This link seems to describe the best way to handle my problem: http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/06/29/how-we-do-mvc-view-models.aspx
JK
A: 

This is still a view.

You are not specifying what kind of storage are you using, I'll make an example using the entity framework, but you can do it with whatever method you like.

The model for the view is an Entity. When you display the form in your view, only part of the fields in your model are editable. When the user submits, therefore, your model has only several fields filled in.

So, you should retrieve a new copy of the object you are editing from the database (call it "fromDb"), copy only the edited fields into the fromDb object, and save the fromDb object instead.

This way, all the fields are preserved.

Another way to do this, is to render hidden fields for all the fields that are not present. However this is NOT secure, as the user could edit those fields by hand (using the developer tools, or firebug).

Palantir
Yes, I'm using entity framework, and yes I'm currently using the make a new copy (clone) and only updating the 3 fields that were sent to the view - but this is a horrible way to do it :( Its manual, time consuming and it's easy to miss a field - especially when we will have 100+ views. It doesn't refactor nicely when a new field is added either (since I would have to stop and manually add the new field to my Update action)
JK