title: ASP.Net Filter Dropdown list items from multiple tables
body: <p><strong>Summary:</strong></p>
  
  <p>I'm trying to use two DropDownList controls to filter the data that is currently being sorted and displayed in a view.</p>
  
  <p><strong>What we are going to learn</strong>
  Creating the ViewController for One to Many and Many-to-Many relationships that could Filter the data using DropDownList </p>
  
  <p><strong>Possible Causes</strong>
  If my DropdownList code is not terrible wrong, The ViewModel I'm using to display the data has no proper support for the DropDownList items. </p>
  
  <p>In other words, the RazorView and my ViewModels are not compatible for what I'm trying to achieve. If I try to change my ViewModel or RazorView, I get an eldless loop of errors for my existing code. </p>
  
  <p>OR The Linq Query needs an expert attention</p>
  
  <p><strong>Here is FilterViewModel.cs</strong></p>
  
  <pre><code>    public IEnumerable &lt;App&gt;      Apps { get; set; }
      public IEnumerable &lt;Language&gt; Languages { get; set; }
      public IEnumerable &lt;Platform&gt; Platforms { get; set; }
      public IEnumerable &lt;AgeGroup&gt; AgeGroups { get; set; }
      public IEnumerable &lt;ProductCode&gt; ProductCodes { get; set; }
  </code></pre>
  
  <p><strong>Here is AppsController.cs</strong></p>
  
  <pre><code> public ActionResult FilterApps(App app)
      {
          var apps = _context.Apps.ToList();
          var languages = _context.Languages.ToList();
          var productCodes = _context.ProductCodes.ToList();
          var platforms = _context.Platforms.ToList();
          var ageGroups = _context.AgeGroups.ToList();
          var viewModel = new FilterViewModel
          {
              AgeGroups = ageGroups,
              Languages = languages,
              Platforms = platforms,
              ProductCodes = productCodes,
              Apps = apps
              .Where(a =&gt; a.LanguageId == app.LanguageId &amp;&amp; a.PlatformId == app.PlatformId)
       // I also tried all possible combinations :(a.Lanage.Id etc)
          };
          return View("FilterApps", viewModel);
      }
  </code></pre>
  
  <p><strong>Here is the FilterApps.cshtml</strong></p>
  
  <pre><code>@model Marketing.ViewModels.FilterViewModel
  &lt;h2&gt;FilterApps&lt;/h2&gt;
  @using (Html.BeginForm("FilterApps", "Apps", FormMethod.Post, new { enctype = "multipart/form-data" }))
  {
  &lt;div class="form-group"&gt;
  
      @Html.DropDownListFor( m =&gt; m.Languages,
        new SelectList(Model.Languages, "Id", "Name"),"Select Language",
        new { @class = "form-control", @id = "dropDown" })
  
      @Html.DropDownListFor(m =&gt; m.Platforms, 
        new SelectList(Model.Platforms, "Id", "Name"), "Select Platform",
        new { @onchange = "this.form.submit();",
        @class = "form-control", @id = "dropDown" })
  
  &lt;/div&gt;
  }
  
  //The existing code below is working fine so far.
  @foreach (var group in Model.AgeGroups)
    {
     &lt;h4&gt;@group.Name&lt;/h4&gt;
  
       @foreach (var app in Model.Apps.OrderBy(a =&gt; a.AppOrder))
              {
                  if (app.AgeGroupId == group.Id)
                  {
                   @app.ProductCode.Name
                   @app.Name
                   @app.Platform.Name
                  }
               }
     }
  </code></pre>
  
  <p>Probably unnecessary but I hope the additional information will help.</p>
  
  <p><strong>Additional Information</strong></p>
  
  <p>The App.cs is referencing all other tables e.g.</p>
  
  <pre><code>public Language Language { get; set; }
  public int LanguageId { get; set; }
  
  public Platform Platform { get; set; }
  public int PlatformId { get; set; } 
  and so on... 
  </code></pre>
  
  <p><strong>What I have already tried</strong>
  Several breakpoints and Logs to track the data, I also tried to use the following but it ruins my existing sorting and grouping.</p>
  
  <pre><code>public App App { get; set; } //Instead of the IEnumerable&lt;App&gt; 
  </code></pre>