Im trying to generate a random ID number so when the user clicks a button it redirects to a random artist. I was able to do it and it works quite well with the code below.
var artists = _context.Artists;
var totalArtists = artists.Count();
Random randomArtistNumber = new Random();
int randomArtistID = randomArtistNumber.Next(1, totalArtists + 1);
if (button.Equals("btnArtist"))
{
return RedirectToAction("Details", "ArtistModels", new { id = randomArtistID.ToString() });
}
The problem here is that if the user deletes an artist then im going to have an ID number that no longer exists in the random list created above. How would I go about creating a List<> of all ArtistID's in the Artist Table? It would be much better to just pick a random number from within a list of active ID's
ArtistModel below
public class ArtistModel
{
[Key]
public int ArtistID { get; set; }
public string ArtistName { get; set; }
public string CountryOfOrigin { get; set; }
public int SubgenreID { get; set; }
public SubgenreModel Subgenre { get; set; }
public ICollection<AlbumModel> Albums { get; set; }
}