Create Viewmodel from Controller / Model. Populate lists from database

entity-framework entity-framework-6 mvvm viewmodel wpf

Question

I'm working on my first WPF MVVM application. I created a database and wrote queries to fetch album names and song names. Now I want to fill a list in my View with the album names and a second list with corresponding songs. I'm new to c# and WPF. I'd like to know how a view model would like for my controller would look like.

My controller:

    public class BandManagerController
    {
        private bandkramEntities _context;

        public BandManagerController()
        {
            _context = new bandkramEntities();
        }

        public List<AlbumData> GetAlbumList()     
        {
            return _context.albums
                .Select(a => new AlbumData
                {
                    AlbumID = a.AlbumID,
                    AlbumName = a.AlbumName,
                })
                .ToList();
        }

        public List<SongData> GetSongList(int albumID)
        {
            return _context.songs
                .Where(s => s.AlbumID == albumID)
                .Select(s => new SongData
                {
                    SongID = s.SongID,
                    SongName = s.SongName
                })
                .ToList();
        }
    }

I created a helper class with the NotifyOfPropertyChange class and a song and album data class:

AlbumData.cs

    public class AlbumData
    {
        public string AlbumName { get; set; }

        public int AlbumID { get; set; }
    }

SongData.cs

    public class SongData
    { 
            public string SongName { get; set; }

            public int SongID { get; set; }            
    } 

For a better overview I want to split my Viewmodel into 4 main parts.

  1. SongViewModel.cs
    public class SongViewModel : NotifyOfPropertyChange
    {
        public SongViewModel()
        {

        }

        public string SongName { get; set; }

        public int SongID { get; set; }
    }
  1. AlbumViewModel.cs
    public class AlbumViewModel : NotifyOfPropertyChange
    {
        public AlbumViewModel()
        {

        }

        public string AlbumName { get; set; }

        public int AlbumID { get; set; }
    } 
  1. SongListViewModel.cs
  2. AlbumListViewModel.cs

I would like to know how 3. and 4. would have to look like to fill the album list with the album names and show corresponding songs in a second list.

1
0
10/14/2019 5:46:12 PM

Popular Answer

With MVVM you would lose the controller and just have a ViewModel. There you would have your lists as such(including a notify property changed)

  private ObservableCollection<SongData> _songList;
  public ObservableCollection<SongData> SongList
  {
    get { return _songList; }
    set { SetProperty(ref _songList, value, () => SongList); }
  }

Then you would load this list at some point

public void LoadSongData(int albumID)
    {
     Using(YourContext _context = new YourContext)
     {
        SongList = new ObservableCollection(_context.songs
            .Where(s => s.AlbumID == albumID)
            .Select(s => new SongData
            {
                SongID = s.SongID,
                SongName = s.SongName
            })
            .ToList());
      }
    }
0
10/14/2019 5:03:32 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow