Search code examples
c#asp.netviewmany-to-manyviewmodel

Load data from many to many related tables in one view asp.net


I'm new to asp.net and may be missing some simple solution. So basically i've got two tables Movie and Actor here are their models:

public class Movie
{
    public Movie()
    {
        this.Actors = new HashSet<Actor>();
    }
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Genre { get; set; }
    [Required]
    public DateTime ReleaseDate { get; set; }
    [Required]
    public DateTime AddDate { get; set; }
    public virtual ICollection<Actor> Actors { get; set; }

}

public class Actor
{
    public Actor()
    {
        this.Movies = new HashSet<Movie>();
    }
    public int id { get; set; }
    [Required]
    public string Name { get; set; }
    public virtual ICollection<Movie> Movies { get; set; }
    public DateTime BirthDate { get; set; }
}

I'm trying to load movie details and the list of actors from the movie using ViewModel:

public class MovieDetailsViewModel
{
    public Movie Movie { get; set; }
    public Actor Actor { get; set; }
}

And the controller for it is here:

 private ApplicationDbContext _context;
    public MoviesController()
    {
        _context = new ApplicationDbContext();
    }
    protected override void Dispose(bool disposing)
    {
        _context.Dispose();
    }

    public ActionResult Index() 
    {
        var movie = _context.Movies.ToList();
        return View(movie);
    }
    public ActionResult Details(int id)
    {
        var movie = _context.Movies.SingleOrDefault(m => m.Id == id);
        var actor = _context.Actors.SingleOrDefault(a => a.id == id);

        var viewModel = new MovieDetailsViewModel
        {
            Movie = movie,
            Actor = actor
        };


        if (movie == null)
            return HttpNotFound();
        return View(viewModel);
    }

Action Details is the one that needs to be loading the data that I need. View looks something like this:

@model Movie_Rentals.ViewModels.MovieDetailsViewModel
@{
ViewBag.Title = Model.Movie.Name;
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@Model.Movie.Name</h2>
<ul>
<li>Genre: @Model.Movie.Genre</li>
<li>Release date: @Model.Movie.ReleaseDate.ToShortDateString()</li>
<li>Add date: @Model.Movie.AddDate.ToShortDateString()</li>
<li>Actors:@Model.Movie.Actors</li>
</ul>

And it displays Actors:System.Collections.Generic.HashSet`1[Movie_Rentals.Models.Actor] which I suppose it should, but I can't mannge to find a solution on how to make it to display the actual list of actors. Does anyone have any idea on my issue, I would appreciate any help.

P.S. I'm not a native english speaker, so sorry if my english is bad.


Solution

  • You need to load the Movie with all Actors using

    _context.Movies.Include(x => x.Actors).SingleOrDefault(m => m.Id == id);
    

    Then in the View

    @foreach (var x in Model.Movie.Actors)
        {<li>x.Name</li>}