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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:39:18+00:00 2026-05-29T10:39:18+00:00

My first MVC3 EF 4.2 site and I’m confused on some things, currently on

  • 0

My first MVC3 EF 4.2 site and I’m confused on some things, currently on ViewModels when querying and saving. Please correct me if I explain this poorly, i’m not sure how to term this. The .edmx automatically created the table classes but I read it was better to create a ViewModel, considering I need to join tables to display/edit my Product completely. The controller code below is where I join tables to output a Product to edit, and then save. My question – what is the right way to save the Product, to the Product.cs model generated by DbContext or my own ProductViewModel.cs?

Is there an easier method to query a product and join the tables and then map to the viewmodels parameters, or do I keep doing all this in the controller like below?

I also want to save/update the product each time someone views/clicks on the product, so I wasn’t sure if I create a separate ViewModel for updating just that parameter or again, use the Product model.

Hope that makes sense! I can explain further if needed.

private SiteForgeEntities db = new SiteForgeEntities();

public ActionResult Edit(int id)
    {
        var viewModel = (
            from a in db.Products
            join b in db.Sites
            on a.SiteId equals b.SiteId
            join c in db.Sections
            on a.SectionId equals c.SectionId
            join d in db.Affiliates
            on a.AffiliateId equals d.AffiliateId
            select new ProductViewModel()
            {
                ProductId = a.ProductId,
                Product = a.Product,
                Description = a.Description,
                Image = a.Image,
                Price = a.Price,
                Clicks = a.Clicks,
                Link = a.Link,        
                Site = b.Site,
                Section = c.Section,
                Affiliate = d.Affiliate
            }).Single(x => x.ProductId == id);      

        return View(viewModel);
    }

    [HttpPost]       
    public ActionResult Edit(Product product)
    {
     ...update database...do I pass in and save back to Product or my ProductViewModel
    }
  • 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-29T10:39:20+00:00Added an answer on May 29, 2026 at 10:39 am

    You use ViewModel to pass multiple models to the view, but when you save data, you need to save it to the appropriate model. If you are adding or modifying products, you will add items to products (using your DbContext). If you have one-to-many relationship defined between two models (in your Product.cs model you might have a property declared as:

    public virtual ICollection<SomeOtherModel> SomeOtherData { get; set; }
    

    you can use this to build a table instead of passing everything in a ViewModel. There is a nice tutorial here regarding the CRUD operations using EF4. Have a look at these short tutorials that can give you an idea about your strategy http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc.

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

Sidebar

Related Questions

I'm building my first MVC3 website and have connected my site to a mdf
I'm currently working on my first MVC3 application at work (using the Razor view
I am trying to write some unit tests for my MVC3 project (the first
When looking at questions and answers on this site and reading some of the
In first,I have to create some radio buttons in a mvc3 partial view. When
I have setup EF4.2 (Database First) with my MVC3 site. I have to entities
Hello StackOV experts! I've just completed the core pieces of my first MVC3 site.
I'm building an e-commerce site using C#, MVC3, Entity Framework 4, my first stab
i'm building an MVC3 site and using Knockout / JSON for the first time,
For some reason my ASP.NET MVC3 webpage keeps throwing me NullReferenceExceptions. The weird things

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.