Combinding two ViewModels, 1 containing a single row results, the other containing multiple row results

StackOverflow https://stackoverflow.com/questions/22925170

  •  29-06-2023
  •  | 
  •  

Question

I am trying to figure out how I would combine two of the ViewModels I have created (GameSectionVM and ReleaseInfoVM) and then appropriately filling it with data (which I think I know how to do). Normally I would just copy the code from one to the other but what is getting me hung up is the fact that the GameSectionVM would only contain a single row result of the DB query, while the other VM (ReleaseInfoVM) would contain 0 or more row results. I am also a bit unsure of how I would fill the VM with all the data and just need a confirmation that my code thought is right on that.

GameSectinVM:

public class GameSectionVM
{
    public string GameTitle { get; set; }
    public string LogoFileName { get; set; }
    public string Synopsis { get; set; }
}

ReleaseInfoVM

public class ReleaseInfoVM
{
    public Int16 SectionID { get; set; }
    public string ReleaseName { get; set; }
    public string Platform { get; set; }
    public string Publisher { get; set; }
    public string Location { get; set; }
    [HiddenInput]
    public DateTime? ReleaseDate { get; set; }
    [HiddenInput]
    public string ReleaseDateText { get; set; }
    public string DateReleased
    {
        get
        {
            return (ReleaseDate.HasValue) ? ReleaseDate.ToString() : ReleaseDateText;
        }
    }
}

This is the code I currently use to fill both VMs:

var model = (from s in db.Sections
            join f in db.Files
            on s.LogoFileID equals f.ID into s_f
            where s.RouteName == SectionName
            from x in s_f.DefaultIfEmpty()
            select new GameSectionVM
            {
                GameTitle = s.Title,
                LogoFileName = x.FileName,
                Synopsis = s.Synopsis
            }).Single();
var ReleaseInfo = db.Releases.All(r => r.SectionID == SectionID);

I would imaging the code to fill the ReleaseData would stay the same when the two VMs are combined but instead of var ReleaseInfo it would become model.ReleaseInfo

EDIT: I forgot to note that each game section can have multiple releases, which is why I want to combined the two VMs so a single model passed up to the View will have the Section's Title, Logo, Synopsis, and all the Release information for each release of that game. At the database level the two tables (Section and Release) are tied together by Release.SectionID being a FK to Section.ID

Was it helpful?

Solution

If this is a parent -> child relationship between GameSectionVM and ReleaseInfoVM, then the easiest way to do this would be to add an ICollection<ReleaseInfoVM> to the properties of GameSectionVM.

public class GameSectionVM
{
...
public ICollection<ReleaseInfoVM> ReleaseInfos {get;set;}
}

code to fill the VM:

// this var will evaluate to GameSectionVM
var model = (from s in db.Sections
...
}).Single();

model.ReleaseInfos = (from r in db.Releases
...
// Do your query logic to populate the ReleaseInfoVM tables here
);

OTHER TIPS

Given that GameSectionVM and ReleaseInfoVM don't look anything like each other, I assume what you mean is to create a third ViewModel that has a single GameSectionVM as a property and a collection of ReleaseInfoVM instances as another property.

This should be fairly straightforward:

public class CombinedVM
{ 
    public GameSectionVM GameSection { get;set;}

    public IEnumerable<ReleaseInfoVM> ReleaseInfos { get;set; }
}

... and assuming you have already retrieved the GameSectionVMand the list of ReleaseInfoVM instances, you could construct the model like this:

 var newModel = new CombinedVM() { GameSection = model, ReleaseInfos = ReleaseInfo };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top