1

I have 2 models (Movies and Actors)

I already can retrieve a list of Movie that contains selected Actors. And I can output all of it via Html.DisplayNameFor. But I wanna use DDL to output Icollection Actors instead of cycle with subitem in @Html.DisplayNameFor(model => model.Actors). So I need dropdownlist corresponded toSelectedList Actor. default output is all/ And if I select specific Movie param such Genre or Budget than DDL with Actors should correspond to this.

public class Movie
{
    public int MovieID { get; set; }
    public string Title { get; set; }
    public string  Date { get; set; }
    public int Budget { get; set; }
    public string  Genre { get; set; }
    public virtual ICollection<Actor> Actors { get; set; }
}

public class Actor
{  
    public int ID { get; set;}
    public string Name { get; set; }
    public virtual ICollection<Movie> Movies { get; set; }
}

I realize output with

   <th>
        @Html.DisplayNameFor(model => model.Genre)
    </th>........................

    <th>.........................
        @Html.DisplayNameFor(model => model.Actors)
    </th>...............

With Actors collection I need another form for every row of my output. Because every film can have many actors. If Actors have to much columns how can I hide it? Or output some quantity with ability to expand it? I choose to add them to DropDownList. Maybe some new AJAX form or asp.net element would be good alternative.

Hence I have MovieController:

if (!String.IsNullOrEmpty(SearchActor))
{             
    Movie = Movie.Where(c => c.Actors.Any(i => i.Name.Contains(SearchActor)));
}

return View(Movie.ToList());

It returns a movie. How can I retrieve Actors.Name (string) from Movie corresponding to Actor ICollection?

I would add this string to DDL.

streamc
  • 676
  • 3
  • 11
  • 27
  • I dont quite understand the question? Do you want the first Actors name or a List of actors by movie? as you say you need a string – SWilko Jun 18 '16 at 19:54
  • I already can retrieve a list of Movie that contains selected Actors. And I can output all of it via Html.DisplayNameFor. But I wanna use DDL to output Icollection Actors instead of cycle with subitem in @Html.DisplayNameFor(model => model.Actors). So I need dropdownlist corresponded toSelectedList Actor. default output is all/ And if I select specific Movie param such Genre or Budget than DDL with Actors should correspond to this. Thanks! A try to be more clear. I edit my question. – streamc Jun 18 '16 at 22:04
  • What exactly is your question? How to show a drop-down list? – Gert Arnold Jun 18 '16 at 22:28
  • Output drop-down list with dynamically changing Actors Collection depending on Movie I select, – streamc Jun 18 '16 at 22:30

1 Answers1

1

Try this

var actors = Movie.Where(c => c.Actors.Any(i => i.Name.Contains(SearchActor))).SelectMany(i => i.Actors);
neer
  • 4,031
  • 6
  • 20
  • 34
  • I made this earlier. But how can I add it to Dropdownlist? var AllActors = Movie.Where(c => c.Actors.Any(i => i.Name.Contains(SearchActor))).SelectMany(u => u.Actors); var ActorLst = new List(); ActorLst.AddRange(AllActors.Distinct()); And I get conversion Error – streamc Jun 18 '16 at 22:39
  • 1
    Check out http://stackoverflow.com/questions/18382311/populating-a-razor-dropdownlist-from-a-listobject-in-mvc @ifooi – neer Jun 18 '16 at 22:43
  • OK, Thanks. I change list of string to list of Actor. And I would read. – streamc Jun 18 '16 at 22:48
  • 1
    Movie.Where(c => c.Actors.Any(i => i.Name.Contains(SearchActor))).SelectMany(i => i.Actors).Select(i => new { i.Name}) @ifooi – neer Jun 18 '16 at 22:54
  • var AllActors = Movie.Where(c => c.Actors.Any(i => i.Name.Contains(SearchActor))).SelectMany(i => i.Actors).Select(i => new { i.Name }); var ActorLst = new List(); ActorLst.AddRange(AllActors.Distinct()); cannot convert from 'System.Linq.IQueryable<>' to 'System.Collections.Generic.IEnumerable' Sorry men. And Thabks. – streamc Jun 18 '16 at 22:58
  • 1
    Just chage **new { i.Name}** to **i.Name** @ifooi – neer Jun 18 '16 at 23:08
  • Yes, it works correctly and halfway done but I need to think more total. With var AllActors = Movie.Where(c => c.Actors.Any(i => i.Name.Contains(SearchActor))).SelectMany(i => i.Actors).Select(i => i.Name); I can output actors for only selected actors. That good but when I select movie that construction outputs all actors despite not all of them exists in Movie, Logically I think I need add more Contains to var. – streamc Jun 18 '16 at 23:23
  • And add some logical conditions. – streamc Jun 18 '16 at 23:30