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 9191409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:40:35+00:00 2026-06-17T20:40:35+00:00

I have recently started working as a web developer. I work with ASP .NET

  • 0

I have recently started working as a web developer. I work with ASP .NET MVC 4 and NHibernate.

At my work-place, we are strictly made to use viewmodels to transfer data to and fro between a controller and a view. And the viewmodels are not supposed to contain any object of a model.
I understand that it is a sort of a tier between the controller and the view.

But I find it repetitive and redundant to write a viewmodel class even if we can directly send the model’s object to the view (in most cases).

For example, if i want to display an order i can do this in the controller’s action –

return View(Repository.Get<Order>(id));

But instead, I have to write a viewmodel, fill it with the fetched order and then pass it to the view.

So, my question is, what purpose does writing viewmodels serve when we can use the model’s object as it is?

  • 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-17T20:40:36+00:00Added an answer on June 17, 2026 at 8:40 pm

    For smaller projects, you’re right. I hear your argument and sympathise – however there are good reasons for this, drudged and repetitive work, especially in larger and more complicated applications:

    • It’s essential to perform all processing within the Controller’s action. However in the example you’ve given, the Repository.Get method might return a lazily-evaluated IQueryable object, which would mean the DB wouldn’t be hit until the View is evaluated. For a variety of reasons this is bad. (A workaround is to call .ToList while still in the controller).
    • “A view should not contain any non-presentational logic” and “You should not trust the View” (because a View could be user-provided). By providing a Model object (potentially still connected to an active DatabaseContext) a view can make malicious changes to your database.
    • A View’s data-to-display does not always map 1:1 with its Model’s data, for example consider a User Details page:

      A User’s EF Model object represents its entity in the database, so it probably looks like this: User { UserId, UserName, PasswordHash, PasswordSalt, EmailAddress, CreatedDate }, whereas the fields on a “User details” page are going to be User { UserId, UserName, Password, ConfirmYourPassword, EmailAddress }, do you see the difference? Ergo, you cannot use the EF User model as the view model, you have to use a separate class.

    • The dangers of model manipulation: if you let ASP.NET MVC (or any other framework) do the model binding to the incoming HTTP POST Request then (taking the User details example above), a user could reset anyone’s password by faking the UserId property value. ASP.NET will rewrite that value during binding and unless you specifically sanitize it (which will be just as drudgeful as making individual ViewModels anyway) then this vulnerability will remain.

    • In projects with multiple developers working in a team situation, is is important that everything is consistent. It is not consistent to have some pages using bespoke ViewModels but other pages using EF Models because the team does not share a concious mind, things have to be documented and generally make-sense. For the same reason a single developer can get away without putting excessive XML documentation in his source code, but in a team situation you’ll fall apart if you don’t.

    There is a slight workaround in your case I’ll share with you, but please note the preconditions:

    • Your views can be fully trusted
    • Your views contain only presentational logic
    • Your application is largely CRUD
    • Your views correspond 1:1 with each EF entity model (i.e. no JOINs)
    • Your views only deal with single Simple models for POST forms, not Complex models (i.e. an object graph)

    …then you can do this:

    • Put all one-way, non-form-related data into your ViewData collection, or the ViewBag in MVC 4 (or even a generic ViewData<T> if you’re hardcore). This is useful for storing HTML page titles and sharing data with Master pages.
    • Use your fully-evaluated and loaded EF models as your View<TModel> models.

    But use this approach with caution because it can introduce inconsistency.

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

Sidebar

Related Questions

This is a very elementary I realize, I have recently started working with asp.net
I have recently started working with Spring MVC framework. I made a lot of
I have recently started teaching myself C# and Asp.net. I am trying to build
I have recently started working on a web application at a new company and
I have started working recently with the new .net feature of bundling and minification
I have recently just started working with Lucene (specifically, Lucene.Net) and have successfully created
Recently I have started working with Codeigniter framework in order to develop RESTFul web
I recently started learning about ASP.Net MVC and its various features MVC_3_MUSIC_STORE + CODE
I recently started working with Eclipse to build Android Applications. I have made a
I've recently started working in ASP.NET and RDBMS.I'm making a test form where I

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.