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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:40:28+00:00 2026-05-31T08:40:28+00:00

I am new to MVC3, so apologies if this is basic, but I couldn’t

  • 0

I am new to MVC3, so apologies if this is basic, but I couldn’t work it out.

I have a view model that includes 3 tables, an ‘Albums’ table with 2 foreign keys (Artist ID and Label ID).

I have a ‘Labels’ controller and a Details method where I display the Label table fields. I am trying to get the distinct ‘Artists’ from the collection of Albums related to the Label.

At the moment, I can get the name of the artists, but one is generated for each album – I have included the code for this scenario below.

I have tried a bunch of different things, like including Artists in the collection and using the Distinct and group by functions, but to no avail. Not sure if it is doable this way, or whether, due to the indirect relationship between the tables, I need to use a different approach.

Any helps is much appreciated.

Controller:

 public ActionResult Details(int id)
    {

        var viewModel = new LabelsDetailsVM();
        viewModel.Lables = db.Labels
            .Include(a => a.Albums)
            .SingleOrDefault(x => x.LabelID == id);

        return View(viewModel);

View:

 @foreach (var artist in Model.Lables.Albums)
    {
        <tr>
            <td>
                @Html.DisplayFor(model => artist.Artist.ArtistName)
            </td>
        </tr>
    }

View Model

   public class LabelsDetailsVM
{
    public Label Lables { get; set; }
    public IEnumerable<Album> Albums { get; set; }
    public IEnumerable<Artist> Artists { get; set; }
}

}

  • 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-31T08:40:29+00:00Added an answer on May 31, 2026 at 8:40 am

    Here’s an example that might get you on the right track:

    view model:

    public class LabelViewModel
    {
        public Label Label { get; set; }
        public IEnumerable<Artist> Artists { get; set; }
    }
    

    Controller:

    public class LabelController : Controller
    {
        public ActionResult Details(int id)
        {
            var label = db.Labels
                .Include(l => l.Albums)
                .SingleOrDefault(l => l.LabelID == id);
    
            if (label == null)
            {
                return HttpNotFound();
            }
    
            var distinctArtists = label
                .Albums
                .Select(a => a.Artist)
                .Distinct(ArtistComparer.Default);
    
            var model = new LabelViewModel
            {
                Label = label,
                Artists = distinctArtists
            };
    
            return View(model);
        }
    }
    

    and the equality comparer used to distinguish between 2 artists used in the controller that could of course be adapted to match your requirements. In this example it considers that 2 artists represent the same entity if they have the same ID. But you could work with some other properties such as the name and so on, all depends on what you need:

    public class ArtistComparer : IEqualityComparer<Artist>
    {
        protected ArtistComparer()
        {
    
        }
    
        private static readonly IEqualityComparer<Artist> _default = new ArtistComparer();
        public static IEqualityComparer<Artist> Default
        {
            get
            {
                return _default;
            }
        }
    
        public bool Equals(Artist x, Artist y)
        {
            if (x != null && y != null)
            {
                return x.ArtistID.Equals(y.ArtistID);
            }
            return false;
        }
    
        public int GetHashCode(Artist obj)
        {
            return obj.ArtistID.GetHashCode();
        }
    }
    

    View:

    @model LabelViewModel
    
    <h3>@Html.DisplayFor(x => x.Label.LabelName)</h3>
    
    <div>Artists</div>
    <table>
        <thead>
            <tr>
                <th>artist name</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var artist in Model.Artists)
            {
                <tr>
                    <td>@artist.ArtistName</td>
                </tr>
            }
        </tbody>
    </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to ModX and have Experirnce with MVC3. I have a PSD that
I apologise if this seems a little basic, but I'm new to WCF -
I have created a new MVC3 project and used the ADO.NET Entity Data Model
Right, I have been tasked with developing a new application in MVC3 that unfortunately
I'm new to MVC3 and I can't figure out how to use checkboxes in
I'm somewhat new to MVC3, so I have a few questions... I am creating
I'm new to MCV and I'm learning MVC3. I created a model and a
i have a problem with the asp.net MVC3. when i create a new project
I'm trying to add a new view, 'Graph', to my asp.net mvc3 under the
When I create a new MVC3 application, add a model, add a [Required] attribute

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.