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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:12:16+00:00 2026-06-16T21:12:16+00:00

This question relates to an MVC4 project with Entity Framework (database first) I am

  • 0

This question relates to an MVC4 project with Entity Framework (database first)

I am passing an (EF) collection of ‘Company’ objects to a view.

I need to show the name of the ‘Primary User’ which is stored as int (foreign key) in each ‘Company’ object.

I am able to get the PrimaryUserID like this :

@foreach (var item in Model)
{
      <div>@item.PrimaryUserID</div>     
}

but I cannot figure out how to access the PrimaryUser object from the ID?

Is there a way to do this without passing a custom model containing the PrimaryUser object to my view?

Any help would be much appreciated!

//EDIT – this is how I currently pass the collection to my view

   public ActionResult Installers(int id = 0)
    {
        CompanyType installer = db.CompanyTypes.Single(ct => ct.Description == "Installer");

        return View(installer.Companies.ToList());
    }
  • 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-16T21:12:17+00:00Added an answer on June 16, 2026 at 9:12 pm

    You can use the Navigation Property in your model.

    Make sure your have included the property in your EF query.
    ( This is called “eager loading” )

    public ActionResult Installers(int id = 0)
    {
        CompanyType installer =
          db.CompanyTypes
            .Include( ct => ct.Companies.Select( c => c.PrimaryUser ) )
            .Single( ct => ct.Description == "Installer" );
    
        return View( installer.Companies.ToList() );
    }
    

    Then just access the property in your markup:

    <div>@item.PrimaryUser.Name</div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question relates to euler project sum-of-primes, and Stream.view , but there is a
This question most closely relates to the asp.net mvc3 framework. It started out as
This question relates to cleaning up the view and giving the controller more of
This question relates to the Mocha testing framework for NodeJS. The default behaviour seems
This question relates to ASP.NET MVC3 using the Razor Engine. What I need to
This question relates to Simulating file system access . I need to choose files
This question relates to my answer of another of my question. The original question
This question relates to this question which I asked earlier this week. The answer
This question relates to those parts of the KenKen Latin Square puzzles which ask
James here. This question relates (sort of) to the one I posted a little

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.