Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8489313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:47:41+00:00 2026-06-10T21:47:41+00:00

Possible Duplicate: ASP.NET MVC ViewModel Pattern i am new in ASP.Net MVC. i saw

  • 0

Possible Duplicate:
ASP.NET MVC ViewModel Pattern

i am new in ASP.Net MVC. i saw people talk about ViewModel usage in asp.net mvc but things is actually not clear to me why i should create ViewModel classes.

it would be better if some one shed some light on the real life use of ViewModel and also explain with simple code for ViewModel. thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:47:43+00:00Added an answer on June 10, 2026 at 9:47 pm

    ViewModels are great for when your model entities do not correspond one to one with what you want to show on the screen.

    A simple example is an application that stores information about people. Imagine your domain model has a Person entity and an address entity. Each person has many addresses but one default address. In one of your views you want to show the Person’s details as well as the details of their default address. If you want to keep your views strongly typed against a model (which you should) that would be impossible as you can not type a view to 2 entities.

    Here is where viewModels are at their best. You create a new viewmodel class that has 2 properties one of type person and one of type address and in your controller you fill those properties with the needed information and you type your view against this Person+address model class.

    As for the code for them they are just like any other model. An example is coming shortly.

    Domain Models (found in your data access layer maybe, or in a seperate domain model project):

    public class Person{
        public int PersonId{get;set;}
        public string Name {get;set;}
        .
        .
        .
    }
    public class Address{
        public int AddressId{get;set;}
        public string Street1 {get;set;}
        public string Street2 {get;set;}
        .
        .
    }
    

    Then you can have a viewModel class like so:

    public class MyViewModel{
        public Person ThePerson{get;set;}
        public Address TheAddress{get;set;]}
    }
    

    Or even

    public class MyViewModel : Person{
        public Address TheAddress{get;set;]}
    }
    

    But I prefer the first solution while a colleague chooses the second one regularly. Your views would then be strongly typed against MyViewModel and the values accessed in the view through syntax like: @model.Person.PersonId

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Traditional ASP .NET vs MVC I am new to .net world coming
Possible Duplicate: When should I create new Controller class in Asp.net MVC (Design Question)?
Possible Duplicate: ASP.NET MVC - Linq to Entities model as the ViewModel - is
Possible Duplicate: Difference between DropDownlist or DropDownListFor Html helper I'm new in asp.net mvc
Possible Duplicate: ASP.NET MVC: No parameterless constructor defined for this object I'm working on
Possible Duplicate: Client Id for Property (ASP.Net MVC) In my View I'm using jquery
Possible Duplicate: How to: Back button support “Ajax” I have a ASP.NET MVC implementation
Possible Duplicate: Can I use ASP.NET MVC together with regular ASP.NET Web forms Let's
Possible Duplicate: What is a CAPTCHA that is compatible with ASP.NET MVC ? Please
Possible Duplicate: Does anyone beside me just NOT get ASP.NET MVC? I dont know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.