Search code examples
asp.netlinqentity-framework-4html.dropdownlistfor

Output ICollection dropdownlist method corresponding to collection form query


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.


Solution

  • Try this

    var actors = Movie.Where(c => c.Actors.Any(i => i.Name.Contains(SearchActor))).SelectMany(i => i.Actors);