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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:19:14+00:00 2026-05-27T04:19:14+00:00

I am writing an application in MVC2 using Entity Framework As I know ViewModel

  • 0

I am writing an application in MVC2 using Entity Framework

As I know ViewModel has to contain only data without any logic to database. Suppose I have Product class that is ADO.NET entity that has EntityCollection<ProductToStatus> when ProductToStatus is many-to-many table. I have ProductModel (that takes Product in its .ctor) which is passed to View.

public class ProductModel
{
    ....
    public Product Item {get; private set;}
    ...
    public ProductModel(Product item)
    {
         ...
         this.Item = item;
         ...
    }
}

In View I need to render all statuses of the product, so to do it I need to query the DB by item.ProductToStatus.Select(s=>s.ProductStatus).ToList(); in the ProductModel, but this sends request to the DB and thus does it violates the MVC principle?

Is this OK or I need to do something?

  • 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-05-27T04:19:15+00:00Added an answer on May 27, 2026 at 4:19 am

    you shouldn’t do this. Your controller should collect the data required for your view and should package it up and pass it to the view for it to render.

    So your ProductModel should either take the details of the Product it needs in its constructor or through properties (my preference) or should, at a push, use the Product it is given to do all the querying in the constructor to set all of its internal fields but not keep a reference to the Product around. I don’t like the using the Product in the constructor particularly as its doing work in the constructor which is not great, but depending on what it is doing exactly it might be ok.

    Its probably better to make your ProductModel have a load of properties, then you can create it like so:

    var model = new ProductModel()
       {
           Statuses=product.ProductToStatus.Select(s=>s.ProductStatus).ToList(),
           Name=product.Name,
           OtherProperty=GetPropertyValue(product),
           //etc
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a log parser for an asp.net mvc2 web application. I use Entity
I am writing application in C# using GTK# GUI framework to make it cross-platform.
I'm writing an application to start and monitor other applications in C#. I'm using
Iam writing an Application Sharing application, my company has used to choose Adobe Air
I am writing application using c++, in windows. I want to get a thumbnail
Background I'm writing application for android, using Eclipse in Windows. I'm implementing C code
I am writing application using PhoneGap on Android. I need to call some PhoneGap
Am writing an application for iphone to read a text file using NSData. NSFileHandle
I'm writing a hello world example using ASP.net MVC 2 framework with two text
I'm writing application for downloading CSV file from web and inserting data into table

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.