开发者

ASPNET MVC what do you call this, a partial view? Or something else?

开发者 https://www.devze.com 2022-12-27 23:10 出处:网络
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'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.


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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消