Can't access object attributes using X.PagedList

I have a search field that returns a list of movies. It works fine if I reference the model as @MovieSearch in the View. But I want to use pagination so it won't break with thousands of results if someone types "a". I'm trying to use X.PagedList, but when I use IPagedList<MovieSearch> as model I can't access the MovieSearchResults[] results property inside MovieSearch in the View, and that contains most of the data I want to retrieve from the search. Basically I can't access anything inside "results" below. public class MovieSearch { public int? page { get; set; } public MovieSearchResult[]? results { get; set; } public int? total_pages { get; set; } public int total_results { get; set; } } public class MovieSearchResult { public string? backdrop_path { get; set; } public string? first_air_date { get; set; } public int[]? genre_ids { get; set; } public int? id { get; set; } public string? title { get; set; } public string[]? origin_country { get; set; } public string? original_language { get; set; } public string? original_title { get; set; } public string? overview { get; set; } public float? popularity { get; set; } public string? poster_path { get; set; } public float? vote_average { get; set; } public int? vote_count { get; set; } } The search results come as objects from a SearchMovies method, I'm not accessing any local DB. This model comes from the API. Any ideas?
2 Replies
Saber
Saber13mo ago
Sounds to me like your not using the IPagedList correctly. It should probably be IPagedList<MovieSearchResult> as the page list itself will already contain the pagination inormation, so having your own class that has paging info doesn't make sense.
Murilo Barbosa
Murilo BarbosaOP13mo ago
I think you are right. But now I'm getting this error InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Threading.Tasks.Task1[X.PagedList.IPagedList1[Cinemanage.Models.TMDB.MovieSearchResult]]', but this ViewDataDictionary instance requires a model item of type 'X.PagedList.IPagedList`1[Cinemanage.Models.TMDB.MovieSearchResult]'. This is how I'm returning it from the Action: if (!String.IsNullOrEmpty(searchTerm)) { movies = await _tmdbMovieService.SearchMoviesAsync(searchTerm); } var movieResults = movies.results.ToPagedListAsync(pageNumber, pageSize); return View(movieResults); I think it doesn't like the "movieResults = movies.results" But otherwise I can't use the ToPagedListAsync Oh Just needed the await Hehe, all good now. Damn I need some time away. Been grinding Thanks
Want results from more Discord servers?
Add your server