I’m working on an MVC web application using C# and Entity Framework. I have a Movie class with a GenreId property, and a Genre class with an Id property. I’m trying to display a list of movies for each genre on a web page.
Here’s what I have so far:
public class Movie`
{
public int Id { get; set; }
public string Title { get; set; }
public int GenreId { get; set; }
public virtual Genre Genre { get; set; }
// Other movie properties
}
public class Genre
{
public int Id { get; set; }
public string Genre1 { get; set; }
// Other genre properties
}
I’ve tried using the following method in my GenreRepository to get movies for a specific genre:
public IQueryable<Movie> GetGenresWithMovies(int genreId)
{
return _dbContext.Movies.Where(x => x.GenreId == genreId);
}
I’ve tried using the following method in my GenreViewmodel:
private GenreRepository _repo;
public List<Genre> GenreList { get; set; }// property
public List<Movie> MoviesList { get; set; }//property
public Movie CurrentMovie { get; set; }//property
public Genre CurrentGenre { get; set; } //property
public bool IsActionSuccess { get; set; }//property
public GenresViewModel(MovieDbContext context) //constructor
{
_repo = new GenreRepository(context);
GenreList = GetAllGenres();
CurrentGenre = GenreList.FirstOrDefault();
MoviesList = GetMoviesByGenreId(CurrentGenre.Id);
}
public List<Movie> GetMoviesByGenreId(int genreid) //method`
{
return _repo.GetMoviesByGenreId(genreid).ToList();
}
LONDON T RICHARDSON is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.