Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Note this doesnt crash or anything, its just not working how I want it to here is an IMG.
http://s27.postimg.org/vsocn02eb/Untitled.png[^]
What I want it to do is only show movies where Jennifer Garners Id is in the DB so say she was in shrek,I want it to show her name with shrek only.

I have created a SQL Database with 3 tables. Tables are set up like:

Movies:
Id,
Name,
etc
etc

Actor:
Id,
Name

ActorsInMovies:
MovieId,
ActorId

I added them into Visual studio and they come up like this:
C#
namespace MvcMovieDemo.Data
{
    using System;
    using System.Collections.Generic;
    
    public partial class Actor
    {
        public Actor()
        {
            this.Movies = new HashSet<movie>();
        }
    
        public int Id { get; set; }
        public string Name { get; set; }
    
        public virtual ICollection<movie> Movies { get; set; }
    }
}
namespace MvcMovieDemo.Data
{
    using System;
    using System.Collections.Generic;
    
    public partial class Movie
    {
        public Movie()
        {
            this.Actors = new HashSet<actor>();
        }
    
        public int Id { get; set; }
        public string Name { get; set; }
        public string Genre { get; set; }
        public string Rating { get; set; }
        
        public virtual ICollection<actor> Actors { get; set; }
    }
}


I have my controller setup like:
C#
var actor = db.Actors.Single(a => a.Id == id);
model.PageTitle = actor.Name + "'s' Movies";


The whole idea is to have a details page for actors, so you hit id 19 = emma watson I would like to show her movies.

I tried but failed hard here is my controller table:
C#
public ActionResult Movies(int id)
{
    var model = new MoviesViewModel();
    var actor = db.Actors.Single(a => a.Id == id);
    model.PageTitle = actor.Name + "'s' Movies";

    model.Actorname = actor.Name; // Set this so we can output their name in the view.
    var items = db.Movies.OrderBy(i => i.Name).AsQueryable(); // this is here as I will be using PagedView later on

    if (!String.IsNullOrEmpty(actor.Name))
    {
        items = items.Where(i => actor.Id == id);
    }
    model.MovieList = items.ToList();

    return View(model);
}
Posted
Updated 6-Jun-14 1:47am
v5
Comments
ZurdoDev 6-Jun-14 7:43am    
Have you debugged it to see what the issue is?
michaelsalter92 6-Jun-14 7:46am    
It doesnt crash or anything, I just im unsure how I set it up to show The Actors on the left under name Column which it does, then on the right show the list of movies ONLY what they have been in. Here is a screenshot how it looks.
http://s27.postimg.org/vsocn02eb/Untitled.png
as you can see the top has the id 3 which is Jennifers Id, but now I need to set ONLY the movies she has been in on the right.
Paulo Augusto Kunzel 6-Jun-14 8:25am    
I'm sorry michael, but your reply didn't answer Ryan's question. Have you debugged it? through debugging you can find errors in logic because it allows you to slow down. Also, please try to change the "VAR" by what you really are expecting to get.. I might be wrong, but sometimes this is simply a casting issue
michaelsalter92 6-Jun-14 8:32am    
I have stepped through yet like I said the issue is I just coded it completely wrong I think, what its doing is just grabbing name direct, and then grabbing every movie. I just need help to use the relationship I set up

1 solution

Where you have

C#
items = items.Where(i => actor.Id == id);


Which is just plain wrong, methinks.

What you need to be doing is more like

items = items.Where(i=>i.Actors.Contains(id)

(sorry - that's not valid Linq but you hopefully see what I mean! - not in front of a PC at the mo and Linq not my strong suit!)

So you want the list of movies where that movie's collection of actors includes the actor with the Id you specified...
 
Share this answer
 
Comments
michaelsalter92 6-Jun-14 11:08am    
Thats ideally what I want yeah looking at it, on the left we have an actor, on the right we want the movies they have been in.

Just dont know how to filter to the movies just this "actor" has been in, Your method makes sense but im still struggling with code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900