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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:07:59+00:00 2026-06-05T03:07:59+00:00

I know there are a lot of posts on the subject but I cannot

  • 0

I know there are a lot of posts on the subject but I cannot find one that helps me do what I want. I know that I will eventually be using Automapper but before I start playing with it, I want to learn how to do things manually. I want to create a ViewModel, populate it with values from my entities by way of a repository and send it to my View. As simple as this sounds, I am stuggling to get it done. I’m using MVC 3, EF 4.3, Database First. I have auto-generated my classes. I’m posting the relevant entities (abbreviated/renamed for this post) and classes, here is what I have so far:

Aggregate Entity: Shipping Header

using System;
using System.Collections.Generic;

namespace My.Models
{
public partial class ShippingHdr
{
    public ShippingHdr()
    {
        this.ShippingLI = new HashSet<ShippingLI>();
    }

    public int ID { get; set; }
    public int ShipToSiteID { get; set; }
    public Nullable<System.DateTime> DateShipped { get; set; }
    public Nullable<System.DateTime> EstDeliveryDate { get; set; }
    public string FromSitePOC { get; set; }
    public Nullable<int> ShipperID { get; set; }
    public string TrackingNo { get; set; }
    public string Comments { get; set;}
    public virtual Shippers Shippers { get; set; }
    public virtual ICollection<ShippingLI> ShippingLI { get; set; }
}

}

Here is my ViewModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace My.Models.ViewModels
{

public class ShippingHeaderSummaryVM
{
    public int ID { get; set; }
    public string Site { get; set; }
    public Nullable<System.DateTime> DateShipped { get; set; }
    public Nullable<System.DateTime> EstDeliveryDate { get; set; }
    public string TrackingNo { get; set; }
    public string HeaderComments { get; set; }
    public string Shipper { get; set; }
    public int NumOrders { get; set; }
    public string Site { get; set; }


}

}

Here is a query I got to return the items I want to use to populate my Viewmodel with. I believe the best place for this is in a Repository. I verified it returns the data I want using LinqPad (hence the missing reference to my dbContxt). I just don’t know how to get the values from the query to the ViewModel:

var shipments = from h in c.ShippingHdrs
                        where (h.ShippingLI.Count > 1)
                        join
                        e in c.vHr_Employees on h.CreatedBy equals e.ID
                        join
                        s in c.Shippers on h.ShipperID equals s.ShipperID
                        join
                        r in vAaiomsSites on h.ShipToSiteID equals r.SiteID

                        select new
                        {
                            h.ID,
                            r.Site,
                            h.EstDeliveryDate,
                            h.DateShipped,
                            h.TrackingNumber,
                            h.HeaderComments,
                            e.LastName,
                            h.ShippingLI.Count,
                            s.Shipper
                                                        };

So what I want to do, again without using Automapper, is to populate the ViewModel with all of the rows from the ShippingHdr entity and pass it to my view.

Here are the filelds that need to be mapped:

ShippingHeaderSummaryVM mapped from shipments

ID = h.ID
Site = r.Site
DateShipped = h.DateShipped
EstDeliveryDate = h.EstDeliveryDate
TrackingNo = h.TrackingNumber
FromSitePOC = e.LastName
NumOrders = h.ShippingLI.Count
Shipper = s.Shipper
HeaderComments = h.HeaderComments

I am stuck here.
How do I populate the ViewModel from the query?
How then do I then call that action from my controller?

I hope I have given enough information, any help would be appreciated.

  • 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-05T03:08:01+00:00Added an answer on June 5, 2026 at 3:08 am

    In order to populate a list of shipments based on your view model object you would need to create a mapping method to map from your collection of shipments from your database to a collection of shipments based on your view model:

    var model = new List<ShippingHeaderSummaryVM>();
    
    foreach(var h in shipments)
    {
    
        var viewModel = new ShippingHeaderSummaryVM
        {
        ID = h.ID
        Site = r.Site
        DateShipped = h.DateShipped
        EstDeliveryDate = h.EstDeliveryDate
        TrackingNo = h.TrackingNumber
        FromSitePOC = e.LastName
        NumOrders = h.ShippingLI.Count
        Shipper = s.Shipper
        HeaderComments = h.HeaderComments
        }
    
        model.Add(viewModel);
    }
    
    return model;
    

    As a side note, this becomes a one liner after you have AutoMapper up and running:

    var model = Mapper.Map<IEnumerable<ShippingHdr>, IEnumerable<ShippingHeaderSummaryVM>>(shipments);
    

    While, learning how to do things manually is great. Manually mapping models doesn’t really benefit you in any way or form. Go with AutoMapper.

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

Sidebar

Related Questions

I know there are a lot of posts about this, but I cannot get
I know that there is a lot of posts about this problem.But can someone
I know that there are a lot of posts about this issue, but I've
I know there are a lot of posts related to this particular error but
I know there's a lot of posts with the same that problem. I just
I know there a lot of similar questions to this, but I didn't find
I know there are a lot of questions around on this subject, but I've
I know there are a lot of posts about git workflows, but I couldn't
I know there has been a lot of posts on this but it still
I know there are a LOT of blog posts on this. But I can't

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.