asp.net mvc 4 - How can I make my ViewModel map to my actual Model? -


i have tab in application, upon clicking which, user's details displayed (using placeholder html attribute) him inside editable text-boxes. user can view them or can edit whichever detail wants to.

now question is: should create view model class scenario or should use actual account model class? guess have create view model class require 'some' properties account model class, if so, how make 'editable' , subsequently, map edited properties(if any) actual account model class?

also, please tell me need store view model class if need create one.

should create view model class scenario or should use actual account model class?

yes, it's best create model represents action need , prevent under , on posting. work on properties expect users work on. example, if have accountmodel has lot of properties , need work on ten of on add action , 5 of properties on edit action, create 2 viewmodels:

// model or entity public class accountmodel {     // list of properties goes here } // view models public class createaccountmodel {     public string username {get;set;}         public string password {get;set;}     public string phone {get;set;} } // model scenario  // want users edit basic info // not password (e.g. have separate // functionality changing password) public class editaccountmodel {     public string username {get;set;}         public string phone {get;set;} } 

now map viewmodels actual model or entity, can use mapper tools or on own (tiring option small models). here steps:

  • you receive model/entity post
  • you query entity database
  • you copy values viewmodel model/entity
  • you save model/entity database

where need store view model class

you can have in mvc project under models folder created. more of preference , there no standard way of doing - if start layering application. put somewhere makes more sense.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -