When you return the artist list, you need to check the null value and return the new artist list.
This is best done IMO by returning what is called a ViewModel.
Class MyFormViewModel List<Artist> artists {get; set;}
then in your controller
MyFormViewModel fvm = new MyFormViewModel(); fvm.artists = database.Artists.ToList(); if (fvm.artists == null) fvm.artists = new List<Artist>(); return View(fvm)
Then your view is inherited from MyFormViewModel
Then consider the factorization of logic in cntroller, which receives artists and sets objects to another level.
EDIT
The reason for FormViewModel is that if you want to add other things to return to the view, you simply extend the model, making it easier to add more partial views, etc.
EDIT 2
If you have a partial call to ArtistList that accepts a complete list of artists. Then he simply iterates over the artist list and passes another PV call, saying Artist, which is given one instance of the artist.
Then you can do a simple check in the partial view of the Artist for null.
Or you can check the partial view of the ArtistList for a null record and display another PV called say NullArtist.
griegs
source share