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

  • Home
  • SEARCH
  • 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 6073501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:16:40+00:00 2026-05-23T10:16:40+00:00

I am new to MVC and using the repository pattern in an attempt to

  • 0

I am new to MVC and using the repository pattern in an attempt to select data containing two objects to return to a strongly typed viewmodel,

Im getting a bit stuck with what the best way to do this is,

The two tables are related by a customer id field, i have a repository interface set up, and a display template that is strongly typed to a viewmodel that contains properties for the Customer and a Customer Site object, all i need is to display a list of customer sites along with the relevant customer name from the customers table.

In the display template i have the following

<%= Html.DisplayFor(x => x.Customers.CustomerName) %>
<%= Html.DisplayFor(x => x.Customers.Site.AddressLine1) %>

I have this display template working but my model is empty.

Where im getting confused is how to define this data in the interface and repository, and how to return the data to my model, to simply return my list of customers i use this in my interface

IQueryable<Customer> Customers { get; }

Then a simple LINQ select.

But as this data will contain both customers and customer sites im unsure how to define this in the interface?

Also will a LINQ join be a suitable method to return this data to my viewmodel? something like

var Customers =

    from c in customers

    join cs in customerSites on c equals cs.CustomerId into ps

    from p in ps

    select new { Customer = c, cs.CustomerName };

UPDATE=========

This is the code i am using in the view model that is stronly typed to the display template,

public class CustomerViewModel
{
    public int Id { get; set; }
    public string CustomerName { get; set; }
    public string PrimaryContactName { get; set; }
    public SiteViewModel Site { get; set; }
}

Its how to populate the model in the repository/controller with both objects to display in a list that im struggling with.

  • 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-23T10:16:40+00:00Added an answer on May 23, 2026 at 10:16 am

    You may have done soem of the following steps already so please ignore if you have..

    1 creat a ViewModel folder in your solution.

    2 Create a base view model …. might look like this ->

    public class BaseViewModel
        {
            public PageProperties PageProperties { get; set; }
            public User User { get; set; }
        }
    

    3 Setup a view model for your controller action maybe like so ->

    public class ProjectVM : BaseViewModel
        {
            public ProjectPoco project { get; set; }
    
        }
    

    4 In your controller get your data from your repositiory and pass it to an instance of your view model like this ->

    var contextVM = new ProjectVM();

            contextVM.project = ObjectExtensions.Convert<ProjectPoco>(tbl.Single(id));
            contextVM.PageProperties = new PageProperties
                                           {
                                               Heading = contextVM.project.Name,
                                               SubHeading = "Details for" +
    

    contextVM.project.Name
    };
    return View(contextVM);

    5 set your views model to be that of your view model ->

    @model
    NerveCentre.ViewModels.ProjectVM

    6 use your viewmodel to pull data out into your view ->

    @Model.project.Description

    A quick and rough description of passing data to your view via a view model. hope I didnt miss anything out.

    As for the data.. looking at how you have the model (Customers.Site.AddressLine1) would it not just be possible to pass the Customers from your query to your view model?

    So your viewmodel might look something like..

        public class SomeViewModel: BaseViewModel
        {
            public List<Customer> Customers { get; set; }
    
        }
    

    If you let us know what you are using for data access then we might be able to help more with the specifics of getting the data out of your tables and into the format you want?

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

Sidebar

Related Questions

I have developed a fairly small asp.net MVC application using the repository pattern and
I am using a Repository pattern with filters in my MVC application. The project
I am a noob to mvc.I am using a repository pattern with linq2sql using
I have a MVC app using the UnitOfWork, Service Layer, Repository Pattern and EF4
I am using the Service/Repository/EF/POCO pattern in a MVC app, and had a couple
I am fairly new at using the ASP.NET MVC framework and was hoping that
Almost every new Java-web-project is using a modern MVC-framework such as Struts or Spring
I'm trying to understand the Repository Pattern , while developing an ASP.NET MVC application
I am using ASP MVC to develop a new project. I am using the
I'm just about to start a new project on ASP.NET MVC using LINQ to

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.