0
I’m starting to venture out and learn about MVC and C#. I created a movie catalog application with a tutorial, where I have a table in the database called Movie and in the application I do a CRUD in this table. One of the attributes of Movie is Genre, where I am trying to create a table for him and then use the values of this table as an option when filling the Genre field when registering a new Movie.
Below is the query created to search the values of the Genre table in placing them in a list:
public async Task<IActionResult> Index(string movieGenre, string searchString)
{
// Use LINQ to get list of genres.
IQueryable<string> genreQuery = from m in _context.Genre //usando o LINQ para pegar a lista de gêneros.
orderby m.Name
select m.Name;
var movies = from m in _context.Movie //pesquisa LINQ
select m;
if (!string.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
if (!string.IsNullOrEmpty(movieGenre))
{
movies = movies.Where(x => x.Genre == movieGenre);
}
var movieGenreVM = new MovieGenreViewModel
{
Genres = new SelectList(await genreQuery.Distinct().ToListAsync()),
Movies = await movies.ToListAsync()
};
ViewBag.ListaGenre = new SelectList(movieGenreVM.Genres);
return View(movieGenreVM);
}
I’m trying to upload this query to the View, so I can use it on the Create page. Researching came up with the idea of using Viewbag for this.
ViewBag.ListaGenre = new SelectList(movieGenreVM.Genres);
The problem that when I call in the view, he doesn’t show me anything. I made a Bugger and I saw that the Viewbag has the value engraved on it all right, but it is not displayed.
@ViewBag.ListaGenre
I learned that a View page can only use 1 Model, and since this Query is part of another model that is not used by View that I want to print, I’m trying to use Viewbag to just take this query there.
My question is: Am I using Bagview wrong? Did I forget something? Viewbag can only be called if it is recorded on the same page?
Yes! A Dropdownlist. the parameters in the Genreid and Genrename controller are being defined as the fields in the list or must be the same name as in the table?
– Henrique Oliveira Costa