0

I use Entity Framework, so almost all my models are in fact the database. However, sometimes I want to pass to the view a simpler object that is not necessary a database model with all the properties.

For example, I have this model :

int ID
string name
string email
string country
string username
string password

When I pass a model to the login view, I only need the username and password. Thus, I thought about creating a simplified model with only username and password.

My questions :

1) What's the best way to standardize this ? Any best practice ? 2) Can I use inheritance to avoid repeating properties like username / password ? 3) Where I put this non-database model ... in wich cs file ? 4) And what about the name of this simplified model ?

Thank you !

1
  • ViewModels is the way to go. It should contain only the properties concerned with the View. Search for it in google and I am sure you'll find ton of articles. Commented Feb 3, 2014 at 22:03

1 Answer 1

3

Yeah you can use ViewModels.

These are regular classes that contain properties that you need for your view. And these properties could be a subset of a model or the combination of multiple models.

For the login view you could create a LoginViewModel

public class LoginViewModel
{
  public int UserId {get; set;}

  [Required]
  public string Username {get; set;}  //included required attribute for username

  public string Password {get; set;}

}

You can now project your viewmodel from your database query (or any other source).

ViewModels are very helpful as in many cases the views typically don't map one to one with models or may be need annotations that you are not willing to add to your models directly.

So many great uses.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok if I use viewmodels ... now how to link the model and the viewmodel ? Where I put the dataannotations ? Is there any official way or best practice ?
@user3214662 the data annotations should be on the properties of the viewmodel that you require them for .. i'll modify my answer to include some. and you don't need to link the viewmodel and the model, you can get the values from the viewmodel manually or you could use a tool like automapper to get them without having to go through all the properties manually.
"you can get the values from the viewmodel manually " you mean fetching all values from the model in the databasecontext and manually create the viewmodel after ? And not that I need this feature, but if I use a viewmodel instead of the model directly, the entity framework scaffolding feature is useless now, no ?
@user3214662 the scaffold isn't meant for real world apps ... could be used but i doubt they'll serve much purpose besides basic crud apps. Very often you'd want full control of the viewmodel you send to the view. Very often you'll find that what you need to send to the view is either a subset of your original model or a combination of multiple models (tables?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.